多态,object,toString,equals,abstract,javabean的快速生成

本文详细介绍了Java中的多态性概念,包括行为多态、继承与多态的前提、调用方式以及实例。同时讲解了数据类型转换,包括基本类型提升和引用类型的向上、向下转型,强调了转型时的注意事项。此外,还讨论了Object类的toString方法和equals方法的用途及重写。最后,提到了快捷键在生成JavaBean中的应用,以及抽象类的使用规则和注意事项。
摘要由CSDN通过智能技术生成

第一章 多态

1 多态

  • 一种事物的多种形态|表现形式
    行为多态

2 多态前提

  • 继承 | 实现

3 多态的最终表现形式

  • 父类引用指向子类对象

4 多态的调用

  • 正常调用 : p或者s调用的成员 : 1)自己类中存在的成员 2)从父类中继承的成员

  • 多态的调用 : 父类引用调用
    成员变量 :
    编译运行看父类|左边|类型

    ​ 成员方法 :
    ​ 编译看父类|左边|类型,运行找子类|右边|对象

  • 注意 : 如果没有配合方法的重写,多态就没有意义

5 例子

public class Class001_Poly {
    public static void main(String[] args) {
        //对应类型数据赋值给对应类型的变量
        Person p = new Person();
        Student s = new Student();
        //正常调用 : p或者s调用的成员 : 1)自己类中存在的成员  2)从父类中继承的成员


        //多态
        Person  ps=  new Teacher();
        //多态的调用
        System.out.println(ps.str);

        ps.test();

    }
}

class Person{
    String str = "父类";

    public void test(){
        System.out.println("Person----> test");
    }
}

class Student extends Person{
    String str = "子类";

    public void test(){
        System.out.println("Student----> test");
    }
}

class Teacher extends Person{
    String str = "子类2";

    public void test(){
        System.out.println("Teacher----> test");
    }
}


class Animal{}

6 练习

做题四大原则:

  • 继承链:自己没有找父类

​ A

​ |

​ B

​ / \

​ C D

  • 编译看类型、确定方法,运行找对象

  • 就近最优原则

  • 父类引用对子类新增方法不可见

public class Class002_Practice {
	public static void main(String[] args) {
		A a1=new A(); //A and D   A and A
		//多态
		A a2=new B(); //A and D   B and A
		B b =new B(); //B and B   B and A   A and D
		C c=new C();
		D d =new D();
		System.out.println(a1.show(b)); //  A and A B往上找,找A
		System.out.println(a1.show(c)); //  A and A C往上找,找A
		System.out.println(a1.show(d)); //  A and D
		System.out.println(a2.show(b)); //  B and A
		System.out.println(a2.show(c)); //  B and A
		System.out.println(a2.show(d)); //  A and D
		System.out.println(b.show(b));  //  B and B
		System.out.println(b.show(c));  //  B and B
		System.out.println(b.show(d));  //  A and D
	}

}

class A{
	public String show(D obj){
		return ("A and D");
	}
	public String show(A obj){
		return ("A and A");
	}
}

class B extends A{
	//新增方法
	public String show(B obj){
		return ("B and B");
	}
	public String show(A obj){
		return ("B and A");
	}
}

class C extends B{
}

class D extends B{
}

7 数据类型转换

7.1 基本数据类型转换

​ 自动类型提升 : 小 —> 大
​ 强制类型转换 : 大 —> 小
​ 小范围类型 变量 = (小范围类型)大范围类型数据;

7.2 引用 : 转型

  • 小: 子类 大 : 父类

  • 向上转型 : 子类 --> 父类

  • 向下转型 : 父类 --> 子类
    子类类型 变量 = (子类类型)父类引用;
    需求 : 当多态调用不能调用子类独有内容时候,可以向下转型,然后调用子类独有内容

    ​ java.lang.ClassCastException类型转换异常
    ​ 在向下转型的时候,如果抓成其他的子类类型,就会遇到这个异常

  • instanceof 运算符
    引用 instanceof 类型 : 判断前面的引用是否指向后面类型的对象或者后面类型的子类对象,如果是返回true,不是返回fales

7.3 例子

public class Class001_Cast {
    public static void main(String[] args) {
        int i = 1;
        //自动类型提升
        long l = i;
        //强制类型转换
        int i2 = (int)l;


        Zi zi = new Zi();
        //向上转型
        Fu fu = zi;
        //多态
        Fu f = new Zi(); //f对子类新增内容不可见



        //向下转型
        //Zi zi2 = (Zi)f;  //Zi zi2 = new Zi();  --> 可以调用子类独有内容
        //转型转错了,转成了其他的子类类型---> java.lang.ClassCastException类型转换异常

        if(f instanceof Brother){
            Brother zi2 = (Brother)f;  //Brother zi2 = new Zi();  --> 可以调用子类独有内容
            zi2.test();
        }else if(f instanceof Zi){
            Zi zi2 = (Zi)f;
            zi2.test();
        }

        System.out.println(f instanceof Fu);  //T
        System.out.println(f instanceof Zi);  //T
        System.out.println(f instanceof Brother);  //F
    }
}
class Fu{}

class Zi extends Fu{
    String str="Zi";
    void test(){
        System.out.println("子类Zi方法test");
    }
}

class Brother extends Fu{
    String str="Brother";
    void test(){
        System.out.println("子类Brother方法test");
    }
}

第二章 object 与 toString

1 Object

  • 老祖宗类
  • 是java中所有类的父类
  • 如果一个类没有显示的继承父类,默认继承自Object类

2 toString

2.1 理解

  • 返回对象的字符串表现形式

​ public String toString() {
​ return getClass().getName() + “@” + Integer.toHexString(hashCode());
​ }

  • 当输出一个对象 的引用的时候,输出的是对象的引用调用toString方法的返回值==>默认调用toString
  • 为什么要在子类中重写toString方法 :
    因为输出对应的引用时候不想是打印对象的地址值,想要输出对象的所有属性值,对从Object中继承的toString需要,toString实现不满意,在子类中重写toString
  • 注意 : 当定义javabean类型,规范都需要重写toString

2.2 实例

public class Class001_Object{
    public static void main(String[] args) {
        Student s = new Student(1001,"张三丰",100);
        System.out.println(s);
        System.out.println(s.toString());
    }
}

class  Student{
    int no;
    String name;
    int age;

    public Student(){}
    public Student(int no,String name,int age){
        this.no = no;
        this.name = name;
        this.age = age;
    }

    public String toString() {
        return no+"-->"+name+"-->"+age;
    }

}

第三章 equals

1 equals 与 == 的区别

  • == : 比较两个数据是否相等
    基本 : 数据值
    引用 : 对象地址值
  • equals : 比较两个对象数据是否相等
    Object类中的equals方法 : 默认比较对象的地址值
    public boolean equals(Object obj) {
    return (this == obj);
    }

2 重写equals方法

  • 为什么要子类中重写equals方法 :
    使用Object类的equals方法,默认比较对象地址,业务需求想要比较根据对象的属性值比较是否相等,可以在子类中根据需要重写equals,实现比较对象所有属性而非地址值
  • 比较字符串类型的数据 : 使用String重写的equals比较字符串对象的内容而非地址

3 实例

public class Class001_Object{
    public static void main(String[] args) {
        Student s1 = new Student(1001,"张三丰",100);
        Student s2 = new Student(1001,"张三丰",10);
        System.out.println(s1);
        System.out.println(s1.toString());

        System.out.println(s1.equals(""));;
    }
}

class  Student{
    int no;
    String name;
    int age;

    public Student(){}
    public Student(int no,String name,int age){
        this.no = no;
        this.name = name;
        this.age = age;
    }

    public String toString() {
        return no+"-->"+name+"-->"+age;
    }

    //比较字符串类型的数据 : 使用String重写的equals比较字符串对象的内容而非地址
    public boolean equals(Object obj) { //Object obj = new Student(1001,"张三丰",100);
        //增强程序健壮性判断
        if(this==obj){
            return true;
        }
        //两个对象 : this  obj
        //obj向下转型
        if(obj instanceof Student){
            Student s = (Student)obj;
            //两个对象 : this  s
            return this.no==s.no && this.age==s.age && this.name.equals(s.name);
        }
       return false;
    }
}

第四章 快捷键快速生成javabean

public class Car {
    private String brand;
    private String color;
    private double price;

    //空构造
    //快捷键 :  alt+insert-->enter--> tab-->tab-->enter
    public Car() {
    }
    //快捷键 :  alt+insert-->enter--> shift+方向上下键-->enter
    public Car(String brand, String color, double price) {
        this.brand = brand;
        this.color = color;
        this.price = price;
    }

    //设置器访问器
    //快捷键 :  alt+insert-->shift+方向上下键选中setter and getter --> enter--> shift+方向上下键选中属性 -->enter
    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    //快捷键 : alt+insert-->shift+方向上下键选中toString --> enter-->enter
    @Override
    public String toString() {
        return "Car{" +
                "brand='" + brand + '\'' +
                ", color='" + color + '\'' +
                ", price=" + price +
                '}';
    }

	//快捷键 : alt+insert-->equals() and hashcode() --> 一直enter
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Car car = (Car) o;
        return Double.compare(car.price, price) == 0 &&
                Objects.equals(brand, car.brand) &&
                Objects.equals(color, car.color);
    }

}

第五章 abstract抽象类

1 abstract 抽象的

​ 抽象类 : 被abstract修饰的类
​ 抽象方法 : 被abstract修饰的方法
​ 没有方法体
​ 必须存在与抽象类中

2 需求

定义开发部门不同职位工作内容

​ 开发部门 Develop --> work()
​ java攻城狮 : work–> 后端开发
​ web程序猿 : work–> 前端开发

3 注意

  • 抽象类不能实例化
  • 抽象方法必须存在与抽象类中
  • 抽象类中可以定义可以不定义抽象方法,可以定义任意内容
  • 抽象类的使用 :
    1)具体子类对象调用成员
    重写所有的抽象方法 + 按需新增
    2)抽象子类
    按需重写 + 按需新增
  • 抽象方法必须被重写的,但是只需要重写一次,按需重写多次
  • abstract不能与private,final,static,native一起使用
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值