7.instanceof应用之抽象类

instanceof应用

  1. {类型转换} 应用类型,
package opp01.Demon04;
public class Person {
}
-----------------------------------------------------------------------------------------
package opp01.Demon04;
public class Teacher extends Person{
}
-----------------------------------------------------------------------------------------
package opp01.Demon04;
public class Student extends Person {
 }
-----------------------------------------------------------------------------------------

package opp01.Demon04;

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

        //Object > String
        //Object > Person > Teacher
        //Object > Person > Student
        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
        //因为上面三个满足父子关系,所以是true;Object是爷爷;
        //一般通过instanceof来判断类型是否相似
        
      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);//编译就报错,因为他们两个没有关系,是俩条线
    }
}
true
true
true
false
false
=========================================================================
true
true
true
false

注意点:

  1. 父类引用指向子类的对象
  2. 把子类转换为父类,向上转型
  3. 把父类转换为子类,向下转型,强制转换。
  4. 方便方法的调用。
package opp01.Demon04;
public class Person {
    public void run(){
        System.out.println("run");
    }
}
-----------------------------------------------------------------------------------------
package opp01.Demon04;
public class Student extends Person {
  public void go(){
   System.out.println("tangya");
  }
 }
-----------------------------------------------------------------------------------------
package opp01.Demon04;
import opp01.Demon04.Student;
import opp01.Demon04.Person;
import opp01.Demon04.Teacher;
import java.util.Scanner;

public class Application {

    public static void main(String args[]) {

        //类型之间的转换: 父 子
        //高   强制转换       低
         Person  obj=new Student();
        //现在的obj是Person
         //obj将这个对象转换为student类型,我们可以使用student类型方法,就是把person转换成student

      /* Student student= (Student)obj;
         student.go();
         因为obj是Person类型,现在强制转换成Student类型,再赋值给Student
         又因为student是Student类型
         所以student可以调用Student类中的go方法
       */
        ((Student)obj).go();

        //子类转换为父类,可能丢失自己的本来的一些方法!
        Student student=new Student();
        student.go();
        Person person=student;

    }

  /* public static void main(String args[]) {

        //Object > String
        //Object > Person > Teacher
        //Object > Person > Student
        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
        //因为上面三个满足父子关系,所以是true;Object是爷爷;
        //一般通过instanceof来判断类型是否相似

      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);//编译就报错,因为他们两个没有关系,是俩条线
   */
}

tangya

static补充

package opp01.Demon05;
   //2:赋初始值
public class Person {
    {
        System.out.println("匿名代码块");//代码块(匿名代码块)
    }
    //1只执行一次
    static{
        System.out.println("静态代码块");//静态代码块
    }
    //3
    public Person() {
        System.out.println("构造方法");
    }

    public static void main(String[] args) {
       Person person= new Person();
       System.out.println("===============================");
       Person person2=new Person();
    }
}
静态代码块
匿名代码块
构造方法
===============================
匿名代码块
构造方法
-----------------------------------------------------------------------------------------
    package opp01.Demon05;

//静态导入包
import static java.lang.Math.random;//可以把Math省略掉
import static java.lang.Math.PI;
public class Test {
    public static void main(String[] args) {
        System.out.println(random());
        //System.out.println(Math.random());//回返回生成一个随机数
        System.out.println(PI);
    }
}
//fianl修饰常量,如果被修饰了,不能被继承,相当于断子绝孙。
-----------------------------------------------------------------------------------------
    package opp01.Demon05;
//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 ();
        System.out.println(Student.age);//类名.变量
       // System.out.println(Student.score);因为score不是静态变量

        System.out.println(s1.age);
        System.out.println(s1.score);
    }


}
0
 0
 0.0

抽象类

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

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

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

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

5.子类继承抽象类,那么就必须要实现抽象类没有实现的抽象方法,否则该子类也要声明为抽象类。

package opp01.Demon06;

//abstract:抽象类:类(接口)需要被继承,是单继承;但是接口可以多继承。
public  abstract class Action {
    
    //约束~abstract
    //abstract抽象方法,只有方法名字,没有方法的实现。
    public abstract void doSomething();
    //1.不能new这个抽象类,只能靠子类实现它:约束
    //2.抽象类中可以写普通方法。
    //3.抽象方法必须在抽象类中。
}
--------------------------------------------------------------------------------------
    package opp01.Demon06;
//抽象类的所有方法必须有子类实现,继承了它的子类,都必须要实现它的方法。(要重写方法)
public class A extends Action {
    @Override
    public void doSomething() {

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值