### 多态 一个事物有多种表现形态。 - 定义方法时形参为父类,调用方法时传入的参数为子类对象。 - 定义方法时返回值数据类型为父类,调用方法时返回子类对象。 具有继承关系的两个对象可以相互转换,子类转父类即父类引用指向子类对象,可以自动完成,无需强制转换,也叫做向上转型。 父类转子类即子类引用指向父类对象,不能自动完成转换,需要强制转换,也叫做向下转型。 ### 抽象方法和抽象类型 如果一个方法只有方法声明而没有具体的方法实现,那么该方法就是抽象方法,在方法定义处使用 abstract 关键字对其进行修饰。 一旦类中定义了一个抽象方法,则该类也必须声明为抽象类,在类定义处添加 abstract 关键字。 ```java public abstract class Member { public abstract void buyBook(); } ``` 抽象类和普通类的区别在于抽象类不能被实例化。 抽象方法和普通方法的区别在于抽象方法没有方法体。 抽象类中可以没有抽象方法。 但是包含了抽象方法的类必须被定义为抽象类。 可以在抽象类中定义非抽象方法,但是在普通类中不能定义抽象方法。 继承了抽象类的子类必须重写父类的抽象方法,以完成具体的方法实现。 如果子类也是抽象类,则不用重写父类的抽象方法。 ### Object Object 类经常被子类重写的方法: public String toString() 以字符串的形式返回该类的实例化对象信息 public boolean equals(Object obj) 判断两个对象是否相等 public native int hashCode() 返回对象的散列码 ### 包装类 Java 中的数据类型从本质上看可以分为两类:8种基本数据类型和引用类型。 包装类是 Java 提供的一组类,专门用来创建8种基本数据类型对应的对象。 包装类一共有8个,保存在java.lang包中。 byte Byte short Short int Integer long Long float Float double Double char Character boolean Boolean Object:Character、Number、Boolean Number:Byte、Short、Integer、Long、Float、Double 包装类的拆箱和装箱,拆箱和装箱是包装类的特有名词,装箱是指将基本数据类型转为对应的包装类对象,拆箱是指将包装类对象转为对应的基本数据类型。
Cashier.java
public class Cashier { private Member member; public Member getMember() { return member; } public void setMember(Member member) { this.member = member; } // public void settlement() { // this.member.buyBook(); // } public void settlement(Member member) { member.buyBook(); } public Member getMember(String name) { if(name.equals("ordinaryMember")) { return new OrdinaryMember(); }else { return new SuperMember(); } } }
Member.java
public abstract class Member { public abstract void buyBook(); public void test() throws Exception { hashCode(); getClass(); equals(null); clone(); toString(); notify(); notifyAll(); wait(); wait(1000L); } }
OrdinaryMember.java
public class OrdinaryMember extends Member{ @Override public void buyBook() { System.out.println("普通会员买书打6折"); } }
People.java
public class People { private int id; private String name; private int age; private char gender; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public char getGender() { return gender; } public void setGender(char gender) { this.gender = gender; } @Override public String toString() { return "People [id=" + id + ", name=" + name + ", age=" + age + ", gender=" + gender + "]"; } @Override public boolean equals(Object obj) { // TODO Auto-generated method stub People people = (People)obj; if(this.id == people.getId() && this.name.equals(people.getName()) && this.age == people.getAge() && this.gender == people.getGender()) { return true; } return false; } @Override public int hashCode() { // TODO Auto-generated method stub return id*name.hashCode()*age*gender; } }
SuperMember.java
public class SuperMember extends Member{ @Override public void buyBook() { // TODO Auto-generated method stub } }
Test.java
public class Test { public static void main(String[] args) { OrdinaryMember ordinaryMember = new OrdinaryMember(); String str = ordinaryMember.toString(); System.out.println(str); SuperMember superMember = new SuperMember(); System.out.println(superMember); Cashier cashier = new Cashier(); cashier.settlement(superMember); // cashier.setMember(superMember); // cashier.settlement(); OrdinaryMember member = (OrdinaryMember)cashier.getMember("ordinaryMember"); SuperMember member2 = (SuperMember)cashier.getMember("superMember"); Member mem = new OrdinaryMember(); mem = new SuperMember(); // Member mber = new Member(); } }
Test2.java
public class Test2 { public static void main(String[] args) { People people = new People(); People people2 = new People(); // people.equals(people2); // System.out.println(people2.equals(people)); // people.setId(1); // people.setName("张三"); // people.setAge(22); // people.setGender(‘男‘); // System.out.println(people); String str = "Hello"; String str2 = "Hello"; System.out.println(str == str2); System.out.println(str.equals(str2)); String str3 = new String("World"); String str4 = new String("World"); System.out.println(str3 == str4); System.out.println(str3.equals(str4)); // System.out.println(); } }
Test3.java
public class Test3 { public static void main(String[] args) { People people = new People(); People people2 = new People(); people.setId(1); people.setName("张三"); people.setAge(22); people.setGender(‘男‘); people2.setId(1); people2.setName("张三"); people2.setAge(22); people2.setGender(‘男‘); System.out.println(people.equals(people2)); System.out.println(people.hashCode()); System.out.println(people2.hashCode()); } }
原文地址:https://www.cnblogs.com/HiJackykun/p/11172174.html
时间: 2024-11-09 03:44:40