第七天

多态,强制类型转换,方法的重写

package DuoTai;



public class Application {
    public static void main(String[] args) {
//        new Student();
//        new Person();   //一个数据的实际类型是确定的。



        Student student = new Student();
        Person person = new Student();
        Object object = new Student();//引用类型是不确定的,父类或者祖宗类指向子类。


        student.go();
        Student person01 =(Student)person;
        person01.go();
        ((Student) person).go(); // person 转化为 student类型,高转低,强制类型转换。可能会丢失方法。
        Person person1 = student;//student类型,转化为Person类型,低转高,自动转换


        student.run();//子类只能调用自己的方法,或者父类的方法
        person.run();//父类只能调用自己的方法,不能调用子类没有和父类一样的方法。如果子类和父类都有这个方法,那么会执行子类的方法。



        //objict>String 第一条线
        //object>person>student  第二条线
        //object>person>teacher  第三条线
        //System.out.println(x  instanceof  y); x和y,可以比较的条件是,xy在一条线上.如果x和y,与x和new的对象在一条线上,返回值是true。否则是flase
        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 String);//false
        System.out.println(object instanceof Teacher);//false
        System.out.println("================================");


        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("================================");


        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);不在一条线,没办法比较




    }
}

static

变量

private static int age;
private String name;
System.out.println(age);//变量可以直接用
Application application = new Application();
System.out.println(application.name);//必须先创建一个对象,才可以用

方法

public static void run()
{
    System.out.println("run");
}

public void skeep(){
    System.out.println("skeep");
}
    public static void main(String[] args) {
        run();//可以直接用
        new Teacher().skeep();//必须new一个对象才能用
    }
}

代码块

package Static;

public class Teacher {
    {
        System.out.println("匿名代码块");
    }
    static {
        System.out.println("静态代码块");
    }

    public Teacher() {
        System.out.println("构造方法");
    }

    public static void main(String[] args) {
        Teacher teacher = new Teacher();
        System.out.println("=====================");
        Teacher teacher1 = new Teacher();
    }

}
静态代码块
匿名代码块
构造方法
=====================
匿名代码块
构造方法

抽象

abstract

package Abastat;

public abstract class A { //抽象类至少要有一个抽象方法

     public abstract void Say();//抽象一个方法,让儿子去完成,儿子也抽象,让孙子去做,总要有一个做的。

     public A() {
          System.out.println("123");//存在无参构造器
     }
}

package Abastat;

public class Application {
    public static void main(String[] args) {
        B b = new B();//抽象类不能被new,只能new不抽象的。
        b.Say();
    }
}

```
package Abastat;

public  class B extends A {
    @Override
    public void Say() {
        System.out.println("sb");//子类重写父类的方法。
    }

}

posted @ 2024-01-30 20:58  学java的阿驴  阅读(13)  评论(0)    收藏  举报