面向对象11-15


面向对象11-15

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

1.方法名必须相同

2.参数列表必须相同

3.修饰符:范围可以扩大:public > protected >default >private

4.抛出的异常:范围,可以被缩小,但不能放大:ClassNotFoundException > Exception(大)

重写,子类的方法和父类必要一致:方法体不同

为什么要重写:

1.父类的功能,子类不一定需要,或者不一定满足!

alt + Insert : override;

多态

多态注意事项:

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

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

3.存在条件:继承关系,方法需要重写,父类引用指向子类对象

不能被重写的方法

1.static属于类,静态方法,不属于实例

2.final 常量;

3.private方法属于私有

同意方法可以根据发送对象的不同而采用多种不同的行为方式

一个对象的实际类型是确定的,但可以指向对象的引用的类型有很多

public static void main(String[] args) {
    //一个对象的实际类型是确定的
    //new Student();
    //new Person();

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

    //student子类能调用的方法都是子类的或者继承父类的
    Student s1 = new Student();

    //person父类型,可以指向子类,但是不能调用子类独有的方法
    Person s2 = new Student();

    Object s3 = new Student();

    /*
    如果person和student中都有的,只要子类没有重写父类,
    就调用父类的,如果子类重写父类了,则调用子类的方法
     */

    s2.run();//子类重写了父类的方法,执行子类的方法
    s1.run();

    //对象能执行哪些方法,只要看对象左边的类型,和右边关系不大
    //s2.eat();  无法执行
    ((Student)s2).eat();//进行强制转换后可以执行
    s1.eat();
}

instance of (类型转换) 引用类型,判断一个对象是什么类型

public static void main(String[] args) {

    //object 下面有 string
    //object <- person <- teacher
    //object <- person <- student

    Object object = new Student();

    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);//fasle
	
     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);//编译报错

        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);//编译报错

}
//instance of语法:
System.out.println(X instanceof Y);//能不能编译通过
/*
1.父类引用指向子类的对象
2.把子类转换为父类,向上转换
3.把子类转换为父类,向下转型:强制转换
4.方便方法的调用,减少重复的代码

封装、继承、多态! 抽象类接口
*/

static关键字详解

public class Person {
    {
        //代码块(匿名代码块)
    }
    static {
        //静态代码块
    }
}
public class Student {
    private static int age;//静态变量  -- 多线程   ---静态属性
    private double score;//非静态变量

    public void run(){
        go();//非静态方法可以访问类中的静态方法
    }

    public static void go(){

    }

    public static void main(String[] args) {
        /*
        Student s1 = new Student();
        //student.age;
        System.out.println(Student.age); //静态变量推荐使用类名进行访问
        //System.out.println(Student.socre);
        System.out.println(s1.age);
        System.out.println(s1.score);
         */
        new Student().run();//通过new 对象.方法实现
        Student.go();
        go();//静态方法可以调用静态方法
    }
}
import static java.lang.Math.random; //静态导入包
/*
通过final修饰的类不能被继承,没有子类
 */
import static java.lang.Math.PI; //静态导入包

public class Test {
    public static void main(String[] args) {
        //System.out.println(Math.random());
        System.out.println(random());
        System.out.println(PI);
    }
}

抽象类

abstract修饰符可以用来修饰方法也可以修饰类,如果修饰方法,那么该方法就是抽象方法,如果修饰类,那么该类就是抽象类。

抽象类中可以没有抽象方法,但是有抽象方法的类一定要声明为抽象类

抽象类,不能使用new关键字来创建对象,它是用来让子类继承的

抽象方法,只有方法的声明,没有方法的实现,它是用来让子类实现的。

/*
1.不能new这个抽象类,只能靠子类去实现它:约束!
2.抽象类中可以写普通的方法
3.抽象方法必须在抽象类中
抽象的抽象:约束!