Day18-Java

面向对象

instanceof和类型转换

package com.Kong.Oop;

import com.Kong.Oop.demo05.Person;
import com.Kong.Oop.demo05.Student;
import com.Kong.Oop.demo05.Teacher;


public class Application {
    public static void main(String[] args) {

        Object o = new Student();

        //System.out.println(X instanceof Y);能不能编译通过,要看x和y有没有父子关系

        System.out.println(o instanceof Student);//t
        System.out.println(o instanceof Person);//t
        System.out.println(o instanceof Object);//t
        System.out.println(o instanceof Teacher);//f
        System.out.println(o instanceof String);//f
        System.out.println("===========================");
        Person p = new Student();
        System.out.println(p instanceof Student);//t
        System.out.println(p instanceof Person);//t
        System.out.println(p instanceof Object);//t
        System.out.println(p instanceof Teacher);//f
        //System.out.println(p instanceof String);编译错误
        System.out.println("===========================");
        Student s = new Student();
        System.out.println(s instanceof Student);//t
        System.out.println(s instanceof Person);//t
        System.out.println(s instanceof Object);//t
        //System.out.println(s instanceof Teacher);编译报错
        //System.out.println(s instanceof String);编译报错
    }
}
  • 类型转换
  1. 父类的引用指向子类的对象
  2. 把子类转换为父类,向上转型
  3. 把父类转换为子类,向下转型,强制转换
  4. 方便方法的调用,减少重复的代码
public class Application  {
    public static void main(String[] args) {
        //子类转换为父类,可能丢失自己本来的一些方法
        Student student = new Student();
        student.eat();
        Person person = student;
        ((Student)person).eat();
    }
}

static

package com.Kong.Oop.demo06;

//static:
public class Student {
    private double score;//非静态的变量 
    private static int age;//静态的变量

    public void run(){
        go();
    }

    public static void go(){

    }

    public static void main(String[] args) {
        Student s1 = new Student();
        System.out.println(Student.age);
        //System.out.println(Student.score);编译错误
        System.out.println(s1.age);
        System.out.println(s1.score);
        new Student().run();
        go();
    }
}
package com.Kong.Oop.demo06;

public class Person {
    //第一个加载,只执行一次
    static{
        //代码块(静态代码块)
        System.out.println("静态代码块");
    }
    //第二个加载
    {
        //代码块(匿名代码块)
        System.out.println("匿名代码块");
    }
    //第三个加载
    public Person(){
        System.out.println("构造方法");
    }

    public static void main(String[] args) {
        Person p=new Person();
        System.out.println("==========");
        Person p1=new Person();
    }
}
package com.Kong.Oop.demo06;

//静态导入包
import static java.lang.Math.random;
import static java.lang.Math.PI;

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

抽象类

  • abstruct修饰符可以用来修饰方法也可以修饰类,如果修饰方法,那么该方法就是抽象方法;如果修饰类,那么该类就是抽象类
  • 抽象类中可以没有抽象方法,但是有抽象方法的类一定要声明为抽象类
  • 抽象类,不能使用new关键字来创建对象,它是用来让子类继承的
  • 抽象方法,只有方法的声明,没有方法的实现,它是用来让子类实现的
  • 子类继承抽象类,那么必须要实现抽象类没有实现的抽象方法,否则该子类也要声明为抽象类
  • 抽象类有构造器,只是不能自己new,专供子类初始化调用
  • 抽象类存在的核心意义:把子类通用代码抽出来复用,同时定好规矩,强制子类必须实现指定功能,统一整个继承体系
package com.Kong.Oop.demo07;

//abstract 抽象类
public  abstract class Action {

    //约束
    //abstract,抽象方法,只有方法名字,没有方法的实现
    public abstract void dosomething();

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

//抽象类的所有方法,继承了它的子类,都必须要实现它的方法
//类extends是单继承;接口可以多继承
public class A extends Action {
    @Override
    public void dosomething() {}
}
posted @ 2026-05-12 22:15  小空谜  阅读(7)  评论(0)    收藏  举报