面向对象3

封装

  • 该露的露,该藏的藏

    我们程序设计要追求”高内聚,低耦合"。高内聚就是类的内部数据操作细节自己完成,不允许外部干涉;低耦合:仅暴露少量的方法给外部使用。

  • 封装(数据的隐藏)

    通常,应禁止直接访问一个对象中数据的实际表示,而应通过操作接口来访问,这称为信息隐藏。

  • 记住这句话就够了:属性私有,get/set

封装的意义

  • 1.提高程序的安全性,保护数据
  • 2.隐藏代码的实现细节
  • 3.统一接口
  • 4.提高系统可维护性
package com.oop.demo04;

//类 private:私有
public class Student {

    //属性私有
    private String name; //名字
    private String id;   //学号
    private char sex;    //性别
    private int age;     //年龄

    //提供一些可以操作这个属性的方法!
    //提供一些public的get、set 方法

    //get 获得这个属性
    public String getName(){
        return this.name;
    }

    //set 给这个数据设置值
    public void setName(String name){
        this.name = name;
    }

    //alt + insert

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public char getSex() {
        return sex;
    }

    public void setSex(char sex) {
        this.sex = sex;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {

        if(age>120||age<0){ //不合法
            this.age = 3;
        }else {
            this.age = age;
        }
    }
}
package com.oop;


import com.oop.demo04.Student;

//一个项目应该只存一个main方法
public class Application {

    public static void main(String[] args) {

        Student s1 = new Student();

        s1.setName("朴海");
        System.out.println(s1.getName()); // 朴海

        s1.setAge(999); //不合法的
        System.out.println(s1.getAge()); //3

    }
}

继承

  • 继承的本质是对某一批类的抽象,从而实现对现实世界更好的建模
  • extends的意思是扩展。子类的是父类的扩展。
  • Java中类只有单继承,没有多继承!
  • 继承是类和类之间的一种关系。除此之外,类和类之间的关系还有依赖、组合、聚合等。
  • 继承关系的两个类,一个为子类(派生类),一个为父类(基类)。子类继承父类,使用关键字extends来表示。
  • 子类和父类之间,从意义上来讲应该具有"is a"的关系。
  • object 类
  • super
  • 方法重写
package com.oop.demo05;

//在Java中,所有的类都默认直接或间接继承Object类
//Persong 类:父类
public class Person /*extends Object*/{
    
    //public    公有
    //protected 受保护的
    //default   默认
    //private   私有
    private int money = 10_0000_0000;

    public void say(){
        System.out.println("说了一句话");
    }

    public int getMoney() {
        return money;
    }

    public void setMoney(int money) {
        this.money = money;
    }
}
package com.oop.demo05;

//Student 类 is Person:派生类  子类
//子类继承了父类,就会继承父类的全部方法!
public class Student extends Person{
    //Ctrl + H

}
package com.oop.demo05;

//Teacher 类 is Person:派生类  子类
public class Teacher extends Person{
}
package com.oop;


import com.oop.demo05.Student;

//一个项目应该只存一个main方法
public class Application {

    public static void main(String[] args) {
        Student student = new Student();
        student.say();  //说了一句话

    }
}

super和this

super注意点

  • 1、super调用父类的构造方法,必须在构造方法的第一个
  • 2、super必须只能出现在子类的方法或构造器中!
  • 3、super和this不能同时调用构造方法

与this的区别

  • 代表的对象不同
    • this:本身调用者这个对象
    • super:代表父类对象的引用
  • 前提
    • this:没有继承也可以使用
    • super:只能在继承条件才可以使用
  • 构造方法:
    • this():本类的构造
    • super():父类的构造!
package com.oop.demo05_1;

//在Java中,所有的类都默认直接或间接继承Object类
//Persong 类:父类
public class Person /*extends Object*/{

    public Person() {
        System.out.println("Person无参执行了");
    }

    protected String name = "puhai";

    //私有的东西无法被继承!
    public void print(){
        System.out.println("Person");
    }
}
package com.oop.demo05_1;

//Student 类 is Person:派生类  子类
//子类继承了父类,就会继承父类的全部方法!
public class Student extends Person {

    public Student() {
        //隐藏代码:调用了父类的无参构造
        //super(); 调用父类的构造器,必须要在子类构造器的第一行
        System.out.println("Student无参执行了");
    }

    private String name = "xiaobai";

    public void test(String name){
        System.out.println(name);//朴海
        System.out.println(this.name);//xiaobai
        System.out.println(super.name);//puhai
    }

    public void print(){
        System.out.println("Student");
    }

    public void test1(){
        print();      //Student
        this.print(); //Student
        super.print();//Person

    }
    
}
package com.oop;


import com.oop.demo05_1.Student;

//一个项目应该只存一个main方法
public class Application {

    public static void main(String[] args) {
        Student st = new Student();
        st.test("朴海");
        st.test1();

    }
}

方法重写(☆)

重写都是方法的重写,和属性无关

需要有继承关系,子类重写父类的方法!

  • 1、方法名必须相同
  • 2、参数列表必须相同
  • 3、修饰符:范围可以扩大但不能缩小 public>protected>default>private
  • 4、抛出的异常:范围可以被缩小,但不能扩大:ClassNotFoundException --> Exception

总之,子类的方法与父类必须要一致,方法体不同!

为什么需要重写:父类的功能,子类不一定需要,或者不满足!

​ 快捷键 alt+insert ->override;

package com.oop.demo05_2;

//重写都是方法的重写,和属性无关
public class B {

    public void test(){
        System.out.println("B=>test()");
    }

    /* Test1
    public static void test(){
        System.out.println("B=>test()");
    }
     */
}
package com.oop.demo05_2;

public class A extends B{

    //Override 重写
    @Override //注解:有功能的注释!
    public void test() {
        System.out.println("A=>test()");
    }

    /* Test1
    public static void test(){

        System.out.println("A=>test()");
    }
     */
}
package com.oop;

import com.oop.demo05_2.A;
import com.oop.demo05_2.B;

//一个项目应该只存一个main方法
public class Application {

    //静态方法和非静态方法区别很大
    //静态方法(Test1):调用只和左边,即定义的数据类型有关
    //非静态方法:重写,而且只能是public方法
    public static void main(String[] args) {

        //Test2
        A a = new A();
        a.test();  //A=>test()

        B b = new A();
        b.test();  //A=>test()

        /* Test1
        //方法的调用只和左边,即定义的数据类型有关
        A a = new A();
        a.test();  //A=>test()

        //父类的引用指向了子类
        B b = new A();
        b.test();  //B=>test()
         */
    }
}

多态

  • 即同一方法可以根据发送对象的不同而采用多种不同的行为方式。
  • 一个对象的实际类型是确定的,但可以指向对象的引用的类型有很多(父类或有关系的类)

多态存在条件

​ 1、有继承关系

​ 2、子类重写父类方法

​ 3、父类引用指向子类对象

注意事项

  • 1、多态是方法的多态,属性没有多态

  • 2、父类和子类,有联系 ,类型转换异常! ClassCastException

  • 3、存在条件: 继承关系、方法需要重写、父类引用指向子类对象! father f1 = new son();

    不能重写的方法:

    ​ 1、static 方法,属于类,它不属于实例

    ​ 2、final 常量;

    ​ 3、private 方法;

package com.oop.demo06;

public class Person {

    public void run(){
        System.out.println("run");
    }
}
package com.oop.demo06;

public class Student extends Person{

    @Override
    public void run() {
        System.out.println("son");
    }

    public void eat(){
        System.out.println("eat");
    }
}
package com.oop;

import com.oop.demo06.Person;
import com.oop.demo06.Student;

//一个项目应该只存一个main方法
public class Application {

    public static void main(String[] args) {

        //一个对象的实际类型是确定的
        //new Student();
        //new Person();

        //可以指向的引用类型就不确定了:父类的引用指向子类

        //Student 能调用的方法都是自己的或者继承父类的
        Student s1 = new Student();
        //Person 父类型,可以指向子类,但不能调用子类独有的方法
        Person s2 = new Student();
        Object s3 = new Student();

        //对象能执行哪些方法,主要看对象左边的类型,和右边的关系不大!
        s1.run();//son
        s2.run();//son   子类重写了父类的方法,执行子类的方法
        s1.eat();//eat
        //s2.eat()  直接报错,可以改成((Student)s2).eat();

    }

}

instanceof :引用类型,判断一个对象是什么类型

System.out.println(x instanceof y); 

能不能编译通过!取决于x与y之间是否存在父子关系

package com.oop.demo06_1;

public class Person {
    public void go(){
        System.out.println("run");
    }

}
package com.oop.demo06_1;

public class Student extends Person{

}

package com.oop.demo06_1;

public class Teacher extends Person{

}

package com.oop;

import com.oop.demo06_1.Person;
import com.oop.demo06_1.Student;
import com.oop.demo06_1.Teacher;

//一个项目应该只存一个main方法
public class Application {

    public static void main(String[] args) {

        //Object > Person > Student
        //Object > Person > Teacher
        //Object > String
        Object object = new Student();

        //System.out.println(x instanceof y);  能不能编译通过!看x与y之间是否存在父子关系

        System.out.println(object instanceof Student);//true
        System.out.println(object instanceof Person);//true
        System.out.println(object instanceof Object);//true
        System.out.println(object instanceof Teacher);//false
        System.out.println(object instanceof String);//false

        System.out.println("========================");

        Person person = new Student();

        System.out.println(person instanceof Student);//true
        System.out.println(person instanceof Person);//true
        System.out.println(person instanceof Object);//true
        System.out.println(person instanceof Teacher);//false
        //System.out.println(person instanceof String);//编译报错

        System.out.println("========================");

        Student student = new Student();
        System.out.println(student instanceof Student);//true
        System.out.println(student instanceof Person);//true
        System.out.println(student instanceof Object);//true
        //System.out.println(student instanceof Teacher);//编译报错
        //System.out.println(student instanceof String);//编译报错

    }

}

类型转换

package com.oop;

import com.oop.demo06_1.Person;
import com.oop.demo06_1.Student;
import com.oop.demo06_1.Teacher;

//一个项目应该只存一个main方法
public class Application {

    public static void main(String[] args) {
        //类型之间的转化:父  子

        //高              低
        Person st1 = new Student();

        //将st这个对象转换为Student类型,我们就可以使用Student类型的方法了!
        ((Student)st1).go();

        //子类转换为父类,可能丢失自己的本来的一些方法
        Student st2 = new Student();
        st2.go();
        Person pe1 = st2;
        //pe1.go()   编译报错

    }

}
注意事项
  • 1.父类引用指向子类对象
  • 2.把子类转换为父类,向上转型;
  • 3.把父类转换为子类,向下转型; 强制转换
  • 4.方便方法的调用,减少重复的代码!
posted @ 2022-04-11 17:21  lauMonkey  阅读(32)  评论(0)    收藏  举报