## 01、继承性的使用与理解
1、Person 类
| |
| |
| |
| public class Person { |
| |
| String name; |
| private int age; |
| |
| public Person(){ |
| |
| } |
| |
| public Person(String name,int age){ |
| this.name = name; |
| this.age = age; |
| } |
| |
| public void eat(){ |
| System.out.println("吃饭"); |
| sleep(); |
| } |
| |
| private void sleep(){ |
| System.out.println("睡觉"); |
| } |
| |
| public int getAge() { |
| return age; |
| } |
| |
| public void setAge(int age) { |
| this.age = age; |
| } |
| |
| } |
| |
2、Student 类
| |
| |
| |
| public class Student extends Person { |
| |
| |
| |
| String major; |
| |
| public Student(){ |
| |
| } |
| |
| public Student(String name,int age,String major){ |
| this.name = name; |
| |
| setAge(age); |
| this.major = major; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public void study(){ |
| System.out.println("学习"); |
| } |
| |
| public void show(){ |
| System.out.println("name:" + name + ",age = " + getAge()); |
| } |
| |
| } |
| |
3、测试类
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public class ExtendsTest { |
| |
| public static void main(String[] args) { |
| Person p1 = new Person(); |
| |
| p1.eat(); |
| System.out.println("********************"); |
| |
| Student s1 = new Student(); |
| s1.eat(); |
| |
| s1.name = "Tom"; |
| |
| s1.setAge(10); |
| System.out.println(s1.getAge()); |
| |
| } |
| } |
| |


3、Java 中关于继承性的规定
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public class ExtendsTest { |
| |
| public static void main(String[] args) { |
| s1.brease(); |
| |
| Creature c = new Creature(); |
| System.out.println(c.toString()); |
| |
| } |
| } |
| |
4、将上述 Person 类改为如下
| public class Person extends Creature { |
| ... |
| } |
5、Creature 类
| public class Creature { |
| |
| public void brease(){ |
| System.out.println("呼吸"); |
| } |
| } |
| |

1.1、继承性练习
1、练习1

| |
| |
| |
| |
| |
| |
| public class Kids extends ManKind{ |
| |
| private int yearsOld; |
| |
| public Kids() { |
| |
| |
| } |
| |
| public Kids(int yearsOld) { |
| this.yearsOld = yearsOld; |
| } |
| |
| public int getYearsOld() { |
| return yearsOld; |
| } |
| |
| public void setYearsOld(int yearsOld) { |
| this.yearsOld = yearsOld; |
| } |
| |
| public void printAge(){ |
| System.out.println("I am " + yearsOld); |
| } |
| } |
| |
ManKind类
| |
| |
| |
| |
| |
| |
| |
| public class ManKind { |
| |
| private int sex; |
| private int salary; |
| |
| public ManKind() { |
| |
| } |
| |
| public ManKind(int sex, int salary) { |
| this.sex = sex; |
| this.salary = salary; |
| } |
| |
| public void manOrWoman(){ |
| if(sex==1){ |
| System.out.println("man"); |
| }else if(sex==0){ |
| System.out.println("woman"); |
| } |
| } |
| |
| public void employeed(){ |
| if(salary==0){ |
| System.out.println("no job"); |
| }else if(salary!=0){ |
| System.out.println("job"); |
| } |
| } |
| |
| public int getSex() { |
| return sex; |
| } |
| |
| public void setSex(int sex) { |
| this.sex = sex; |
| } |
| |
| public int getSalary() { |
| return salary; |
| } |
| |
| public void setSalary(int salary) { |
| this.salary = salary; |
| } |
| |
| } |
| |
KidsTest
| |
| |
| |
| |
| |
| public class KidsTest { |
| public static void main(String[] args) { |
| |
| Kids someKid = new Kids(12); |
| |
| someKid.printAge(); |
| |
| someKid.setYearsOld(15); |
| someKid.setSalary(0); |
| someKid.setSex(1); |
| |
| someKid.employeed(); |
| someKid.manOrWoman(); |
| } |
| } |
| |
2、练习2

| public class Circle { |
| |
| public double radius; |
| |
| public Circle(){ |
| radius = 1.0; |
| } |
| |
| public double getRadius() { |
| return radius; |
| } |
| |
| public void setRadius(double radius) { |
| this.radius = radius; |
| } |
| |
| public double findArea(){ |
| return Math.PI * radius * radius; |
| } |
| } |
| |
Cylinder类
| public class Cylinder extends Circle{ |
| |
| private double length; |
| |
| public Cylinder(){ |
| length = 1.0; |
| } |
| |
| public double getLength() { |
| return length; |
| } |
| |
| public void setLength(double length) { |
| this.length = length; |
| } |
| |
| public double findVolume(){ |
| return findArea() * length; |
| } |
| } |
| |
测试类
| public class CylinderTest { |
| public static void main(String[] args) { |
| |
| Cylinder cy = new Cylinder(); |
| |
| cy.setRadius(2.1); |
| cy.setLength(3.4); |
| double volues = cy.findVolume(); |
| System.out.println("圆柱的体积:" + volues); |
| |
| double area = cy.findArea(); |
| System.out.println("圆的面积: " + area); |
| } |
| } |
| |
02、方法的重写(override/overwrite)
1、Person类
| public class Person { |
| |
| String name; |
| int age; |
| |
| public Person(){ |
| |
| } |
| |
| public Person(String name,int age){ |
| this.name = name; |
| this.age = age; |
| } |
| |
| public void eat(){ |
| System.out.println("吃饭"); |
| } |
| |
| public void walk(int distance){ |
| System.out.println("走路,走的距离是:" + distance + "公里"); |
| show(); |
| } |
| |
| private void show(){ |
| System.out.println("我是一个人。"); |
| } |
| |
| public Object info(){ |
| return null; |
| } |
| |
| public double info1(){ |
| return 1.0; |
| } |
| } |
| |
2、Student类
| public class Student extends Person{ |
| |
| String major; |
| |
| public Student(){ |
| |
| } |
| |
| public Student(String major){ |
| this.major = major; |
| } |
| |
| public void study(){ |
| System.out.println("学习,专业是:" + major); |
| } |
| |
| |
| public void eat(){ |
| System.out.println("学生应该多吃有营养的。"); |
| } |
| |
| public void show(){ |
| System.out.println("我是一个学生。"); |
| } |
| |
| public String info(){ |
| return null; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| @Override |
| public void walk(int distance) { |
| System.out.println("自动生成"); |
| } |
| } |
| |
3、测试类
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public class PersonTest { |
| |
| |
| public static void main(String[] args) { |
| Student s = new Student("计算机科学与技术"); |
| s.eat(); |
| s.walk(10); |
| |
| s.study(); |
| } |
| } |
| |
2.1、方法重写的细节
1、Person类
| public class Person { |
| |
| String name; |
| int age; |
| |
| public Person(){ |
| |
| } |
| |
| public Person(String name,int age){ |
| this.name = name; |
| this.age = age; |
| } |
| |
| |
| |
| |
| static void eat(){ |
| System.out.println("吃饭"); |
| } |
| |
| public void walk(int distance){ |
| System.out.println("走路,走的距离是:" + distance + "公里"); |
| show(); |
| } |
| |
| private void show(){ |
| System.out.println("我是一个人。"); |
| } |
| |
| public Object info(){ |
| return null; |
| } |
| |
| public double info1(){ |
| return 1.0; |
| } |
| } |
| |
2、Student类
| public class Student extends Person{ |
| |
| String major; |
| |
| public Student(){ |
| |
| } |
| |
| public Student(String major){ |
| this.major = major; |
| } |
| |
| public void study(){ |
| System.out.println("学习,专业是:" + major); |
| } |
| |
| |
| |
| |
| |
| |
| |
| public static void eat(){ |
| System.out.println("学生应该多吃有营养的。"); |
| } |
| |
| public void show(){ |
| System.out.println("我是一个学生。"); |
| } |
| |
| public String info(){ |
| return null; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| @Override |
| public void walk(int distance) { |
| System.out.println("自动生成"); |
| } |
| } |
| |
3、测试类
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public class PersonTest { |
| |
| |
| public static void main(String[] args) { |
| Student s = new Student("计算机科学与技术"); |
| s.eat(); |
| s.walk(10); |
| System.out.println("*******************"); |
| |
| s.study(); |
| |
| Person p1 = new Person(); |
| p1.eat(); |
| } |
| } |
| |
2.2、方法的练习
1、练习1
| 1.如果现在父类的一个方法定义成private访问权限,在子类中将此方法声明为default访问权限,那么这样还叫重写吗?(NO) |
| |
| |
2、练习2
| |
| |
| |
| |
| |
| public class Kids extends ManKind{ |
| |
| private int yearsOld; |
| |
| public Kids() { |
| |
| } |
| |
| public Kids(int yearsOld) { |
| this.yearsOld = yearsOld; |
| } |
| |
| public int getYearsOld() { |
| return yearsOld; |
| } |
| |
| public void setYearsOld(int yearsOld) { |
| this.yearsOld = yearsOld; |
| } |
| |
| public void printAge(){ |
| System.out.println("I am " + yearsOld); |
| } |
| |
| public void employeed(){ |
| System.out.println("Kids should study and no job."); |
| } |
| } |
| |
MindKids类
| public class ManKind { |
| |
| private int sex; |
| private int salary; |
| |
| public ManKind() { |
| |
| } |
| |
| public ManKind(int sex, int salary) { |
| this.sex = sex; |
| this.salary = salary; |
| } |
| |
| public void manOrWoman(){ |
| if(sex==1){ |
| System.out.println("man"); |
| }else if(sex==0){ |
| System.out.println("woman"); |
| } |
| } |
| |
| public void employeed(){ |
| if(salary==0){ |
| System.out.println("no job"); |
| }else if(salary!=0){ |
| System.out.println("job"); |
| } |
| } |
| |
| public int getSex() { |
| return sex; |
| } |
| |
| public void setSex(int sex) { |
| this.sex = sex; |
| } |
| |
| public int getSalary() { |
| return salary; |
| } |
| |
| public void setSalary(int salary) { |
| this.salary = salary; |
| } |
| |
| } |
| |
测试类
| public class KidsTest { |
| public static void main(String[] args) { |
| |
| Kids someKid = new Kids(12); |
| |
| someKid.printAge(); |
| |
| someKid.setYearsOld(15); |
| someKid.setSalary(0); |
| someKid.setSex(1); |
| |
| someKid.employeed(); |
| someKid.manOrWoman(); |
| } |
| } |
| |
03、四种访问权限修饰符
四种权限修饰符

Order类
| package githubb; |
| |
| |
| |
| public class Order { |
| |
| private int orderPrivate; |
| int orderDefault; |
| protected int orderProtected; |
| public int orderPublic; |
| |
| private void methodPrivate(){ |
| orderPrivate = 1; |
| orderDefault = 2; |
| orderProtected = 3; |
| orderPublic = 4; |
| } |
| |
| void methodDefault(){ |
| orderPrivate = 1; |
| orderDefault = 2; |
| orderProtected = 3; |
| orderPublic = 4; |
| } |
| |
| protected void methodProtected(){ |
| orderPrivate = 1; |
| orderDefault = 2; |
| orderProtected = 3; |
| orderPublic = 4; |
| } |
| |
| public void methodPublic(){ |
| orderPrivate = 1; |
| orderDefault = 2; |
| orderProtected = 3; |
| orderPublic = 4; |
| } |
| } |
| |
Ordertest类
| package githubb; |
| |
| public class OrderTest { |
| public static void main(String[] args) { |
| |
| Order order = new Order(); |
| |
| order.orderDefault = 1; |
| order.orderProtected = 2; |
| order.orderPublic = 3; |
| |
| order.methodDefault(); |
| order.methodProtected(); |
| order.methodPublic(); |
| |
| |
| |
| |
| } |
| } |
| |
SubOrder类
| package githubc; |
| |
| import githubb.Order; |
| |
| public class SubOrder extends Order{ |
| |
| public void method(){ |
| orderProtected = 1; |
| orderPublic = 2; |
| |
| methodProtected(); |
| methodPublic(); |
| |
| |
| |
| |
| |
| |
| |
| } |
| } |
| |
OrderTest类
| package githubc; |
| |
| import githubb.Order; |
| |
| public class OrderTest { |
| public static void main(String[] args) { |
| |
| Order order = new Order(); |
| order.orderPublic = 1; |
| order.methodPublic(); |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| } |
| |
| public void show(Order order){ |
| order.orderPublic = 1; |
| order.methodPublic(); |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| } |
| } |
| |
04、关键字:super
Person类
| public class Person { |
| |
| String name; |
| int age; |
| int id = 1003; |
| |
| public Person(){ |
| System.out.println("我无处不在"); |
| } |
| |
| public Person(String name){ |
| this.name = name; |
| } |
| |
| public Person(String name,int age){ |
| this(name); |
| this.age = age; |
| } |
| |
| public void eat(){ |
| System.out.println("人,吃饭"); |
| } |
| |
| public void walk(){ |
| System.out.println("人,走路"); |
| } |
| } |
| |
Student类
| public class Student extends Person{ |
| |
| String major; |
| int id = 1002; |
| |
| public Student(){ |
| |
| } |
| |
| public Student(String name,int age,String major){ |
| |
| |
| super(name,age); |
| this.major = major; |
| } |
| |
| public Student(String major){ |
| this.major = major; |
| } |
| |
| public void eat(){ |
| System.out.println("学生多吃有营养的食物"); |
| } |
| |
| public void Study(){ |
| System.out.println("学生,学习知识。"); |
| this.eat(); |
| |
| super.eat(); |
| super.walk(); |
| } |
| public void show(){ |
| System.out.println("name = " + this.name + ",age = " + super.age); |
| System.out.println("id = " + this.id); |
| System.out.println("id = " + super.id); |
| } |
| } |
| |
测试类
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public class SuperTest { |
| public static void main(String[] args) { |
| |
| Student s = new Student(); |
| s.show(); |
| |
| s.Study(); |
| |
| Student s1 = new Student("Ton",21,"IT" ); |
| s1.show(); |
| |
| System.out.println("***********************"); |
| Student s2 = new Student(); |
| |
| } |
| } |
| |
05、子类对象实例化过程


| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public class InstanceTest { |
| |
| } |
| |
练习
Account类
| |
| |
| |
| |
| |
| |
| |
| |
| public class Account { |
| |
| private int id; |
| private double balance; |
| private double annualInterestRate; |
| |
| public Account(int id, double balance, double annualInterestRate) { |
| super(); |
| this.id = id; |
| this.balance = balance; |
| this.annualInterestRate = annualInterestRate; |
| } |
| |
| public int getId() { |
| return id; |
| } |
| |
| public void setId(int id) { |
| this.id = id; |
| } |
| |
| public double getBalance() { |
| return balance; |
| } |
| |
| public void setBalance(double balance) { |
| this.balance = balance; |
| } |
| |
| public double getAnnualInterestRate() { |
| return annualInterestRate; |
| } |
| |
| public void setAnnualInterestRate(double annualInterestRate) { |
| this.annualInterestRate = annualInterestRate; |
| } |
| |
| public double getMonthlyInterest(){ |
| return annualInterestRate / 12; |
| } |
| |
| public void withdraw (double amount){ |
| if(balance >= amount){ |
| balance -= amount; |
| return; |
| } |
| System.out.println("余额不足"); |
| } |
| |
| public void deposit (double amount){ |
| if(amount > 0){ |
| balance += amount; |
| |
| } |
| } |
| |
| } |
| |
AccountTest类
| |
| |
| |
| |
| |
| |
| public class AccountTest { |
| public static void main(String[] args) { |
| Account acct = new Account(1122,20000,0.045); |
| |
| acct.withdraw(30000); |
| System.out.println("你的账户余额为:" + acct.getBalance()); |
| acct.withdraw(2500); |
| System.out.println("你的账户余额为:" + acct.getBalance()); |
| acct.deposit(3000); |
| System.out.println("你的账户余额为:" + acct.getBalance()); |
| |
| |
| System.out.println("月利率为: " + (acct.getAnnualInterestRate() * 100) + "%"); |
| } |
| } |
| |
CheckAccount类
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public class CheckAccount extends Account{ |
| |
| private double overdraft; |
| |
| public CheckAccount(int id, double balance, double annualInterestRate,double overdraft){ |
| super(id, balance, annualInterestRate); |
| this.overdraft = overdraft; |
| } |
| |
| public double getOverdraft() { |
| return overdraft; |
| } |
| |
| public void setOverdraft(double overdraft) { |
| this.overdraft = overdraft; |
| } |
| |
| @Override |
| public void withdraw(double amount) { |
| if(getBalance() >= amount){ |
| |
| |
| |
| super.withdraw(amount); |
| }else if(overdraft >= amount - getBalance()){ |
| |
| overdraft -= (amount - getBalance()); |
| |
| |
| super.withdraw(getBalance()); |
| |
| }else{ |
| System.out.println("超过可透支限额!"); |
| } |
| |
| } |
| } |
| |
CheckAccountTest类
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public class CheckAccountTest { |
| public static void main(String[] args) { |
| CheckAccount cat = new CheckAccount(1122,20000,0.045,5000); |
| |
| cat.withdraw(5000); |
| System.out.println("您的账户余额为: " + cat.getBalance()); |
| System.out.println("您的可透支额度为: " + cat.getOverdraft()); |
| |
| cat.withdraw(18000); |
| System.out.println("您的账户余额为: " + cat.getBalance()); |
| System.out.println("您的可透支额度为: " + cat.getOverdraft()); |
| |
| cat.withdraw(3000); |
| System.out.println("您的账户余额为: " + cat.getBalance()); |
| System.out.println("您的可透支额度为: " + cat.getOverdraft()); |
| } |
| } |
| |
06、面向对象特征之三:多态性
Person类
| public class Person { |
| String name; |
| int age; |
| |
| public void eat(){ |
| System.out.println("人,吃饭"); |
| } |
| |
| public void walk(){ |
| System.out.println("人,走路"); |
| } |
| |
| } |
| |
Woman类
| public class Woman extends Person{ |
| |
| boolean isBeauty; |
| |
| public void goShopping(){ |
| System.out.println("女人喜欢购物"); |
| } |
| |
| public void eat(){ |
| System.out.println("女人少吃,为了减肥。"); |
| } |
| |
| public void walk(){ |
| System.out.println("女人,窈窕的走路。"); |
| } |
| } |
| |
Man类
| public class Man extends Person{ |
| |
| boolean isSmoking; |
| |
| public void earnMoney(){ |
| System.out.println("男人负责工作养家"); |
| } |
| |
| public void eat() { |
| System.out.println("男人多吃肉,长肌肉"); |
| } |
| |
| public void walk() { |
| System.out.println("男人霸气的走路"); |
| } |
| } |
| |
测试类
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public class PersonTest { |
| public static void main(String[] args) { |
| |
| Person p1 = new Person(); |
| p1.eat(); |
| |
| Man man = new Man(); |
| man.eat(); |
| man.age = 25; |
| man.earnMoney(); |
| |
| |
| |
| Person p2 = new Man(); |
| |
| |
| |
| p2.eat(); |
| p2.walk(); |
| |
| |
| |
| } |
| } |
| |
举例
| |
| |
| |
| public class AnimalTest { |
| |
| public static void main(String[] args) { |
| AnimalTest test = new AnimalTest(); |
| test.func(new Dog()); |
| |
| test.func(new Cat()); |
| } |
| |
| public void func(Animal animal){ |
| animal.eat(); |
| animal.shout(); |
| } |
| |
| |
| public void func(Dog dog){ |
| dog.eat(); |
| dog.shout(); |
| } |
| |
| public void func(Cat cat){ |
| cat.eat(); |
| cat.shout(); |
| } |
| } |
| |
| class Animal{ |
| |
| public void eat(){ |
| System.out.println("动物,进食"); |
| } |
| |
| public void shout(){ |
| System.out.println("动物:叫"); |
| } |
| } |
| |
| class Dog extends Animal{ |
| public void eat(){ |
| System.out.println("狗吃骨头"); |
| } |
| |
| public void shout() { |
| System.out.println("汪!汪!汪!"); |
| } |
| } |
| |
| class Cat extends Animal{ |
| public void eat(){ |
| System.out.println("猫吃鱼"); |
| } |
| |
| public void shout() { |
| System.out.println("喵!喵!喵!"); |
| } |
| } |
| |
6.1、虚拟方法的补充
| import java.util.Random; |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| class Animal { |
| |
| protected void eat() { |
| System.out.println("animal eat food"); |
| } |
| } |
| |
| class Cat extends Animal { |
| |
| protected void eat() { |
| System.out.println("cat eat fish"); |
| } |
| } |
| |
| class Dog extends Animal { |
| |
| public void eat() { |
| System.out.println("Dog eat bone"); |
| |
| } |
| |
| } |
| |
| class Sheep extends Animal { |
| |
| public void eat() { |
| System.out.println("Sheep eat grass"); |
| |
| } |
| |
| } |
| |
| public class InterviewTest { |
| |
| public static Animal getInstance(int key) { |
| switch (key) { |
| case 0: |
| return new Cat (); |
| case 1: |
| return new Dog (); |
| default: |
| return new Sheep (); |
| } |
| |
| } |
| |
| public static void main(String[] args) { |
| int key = new Random().nextInt(3); |
| |
| System.out.println(key); |
| |
| Animal animal = getInstance(key); |
| |
| animal.eat(); |
| |
| } |
| |
| } |
| |
6.2、向下转型的使用
Person 类
| public class Person { |
| String name; |
| int age; |
| |
| public void eat(){ |
| System.out.println("人,吃饭"); |
| } |
| |
| public void walk(){ |
| System.out.println("人,走路"); |
| } |
| |
| } |
| |
Man 类
| public class Man extends Person{ |
| |
| boolean isSmoking; |
| |
| public void earnMoney(){ |
| System.out.println("男人负责工作养家"); |
| } |
| |
| public void eat() { |
| System.out.println("男人多吃肉,长肌肉"); |
| } |
| |
| public void walk() { |
| System.out.println("男人霸气的走路"); |
| } |
| } |
| |
Woman 类
| public class Woman extends Person{ |
| |
| boolean isBeauty; |
| |
| public void goShopping(){ |
| System.out.println("女人喜欢购物"); |
| } |
| |
| public void eat(){ |
| System.out.println("女人少吃,为了减肥。"); |
| } |
| |
| public void walk(){ |
| System.out.println("女人,窈窕的走路。"); |
| } |
| } |
| |
PersonTest 类
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public class PersonTest { |
| public static void main(String[] args) { |
| |
| Person p1 = new Person(); |
| p1.eat(); |
| |
| Man man = new Man(); |
| man.eat(); |
| man.age = 25; |
| man.earnMoney(); |
| |
| |
| System.out.println("************************"); |
| |
| Person p2 = new Man(); |
| |
| |
| |
| p2.eat(); |
| p2.walk(); |
| |
| |
| |
| System.out.println("**************************"); |
| |
| |
| |
| |
| p2.name = "Tom"; |
| |
| |
| |
| |
| |
| |
| Man m1 = (Man) p2; |
| m1.earnMoney(); |
| m1.isSmoking = true; |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| if (p2 instanceof Woman) { |
| Woman w1 = (Woman) p2; |
| w1.goShopping(); |
| System.out.println("**********Woman*********"); |
| } |
| |
| if (p2 instanceof Man) { |
| Man m2 = (Man) p2; |
| m2.earnMoney(); |
| System.out.println("*********Man************"); |
| } |
| |
| if (p2 instanceof Person) { |
| System.out.println("***********Person************"); |
| } |
| |
| if (p2 instanceof Object) { |
| System.out.println("***********object************"); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| Person p4 = new Person(); |
| Man m4 = (Man)p4; |
| |
| |
| Object obj = new Woman(); |
| Person p = (Person)obj; |
| |
| |
| |
| |
| |
| |
| |
| |
| } |
| } |
| |

6.3、多态性的练习
1、练习1
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public class FieldMethodTest { |
| public static void main(String[] args){ |
| Sub s= new Sub(); |
| System.out.println(s.count); |
| s.display(); |
| |
| Base b = s; |
| |
| System.out.println(b == s); |
| System.out.println(b.count); |
| b.display(); |
| } |
| } |
| |
| class Base { |
| int count= 10; |
| public void display() { |
| System.out.println(this.count); |
| } |
| } |
| |
| class Sub extends Base { |
| int count= 20; |
| public void display() { |
| System.out.println(this.count); |
| } |
| } |
| |
2、练习2
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| class Person { |
| protected String name = "person"; |
| protected int age = 50; |
| |
| |
| public String getInfo() { |
| return "Name: " + name + "\n" + "age: " + age; |
| } |
| } |
| |
| class Student extends Person { |
| protected String school = "pku"; |
| |
| public String getInfo() { |
| return "Name: " + name + "\nage: " + age + "\nschool: " + school; |
| } |
| } |
| |
| class Graduate extends Student { |
| public String major = "IT"; |
| |
| public String getInfo() { |
| return "Name: " + name + "\nage: " + age + "\nschool: " + school + "\nmajor:" + major; |
| } |
| } |
| |
| |
| public class InstanceTest{ |
| |
| public static void main(String[] args) { |
| |
| InstanceTest test = new InstanceTest(); |
| test.method(new Student()); |
| |
| } |
| |
| public void method(Person e){ |
| String info = e.getInfo(); |
| System.out.println(info); |
| |
| |
| if(e instanceof Graduate){ |
| System.out.println("a graduated student"); |
| System.out.println("a student"); |
| System.out.println("a person"); |
| }else if(e instanceof Student){ |
| System.out.println("a student"); |
| System.out.println("a person"); |
| }else{ |
| System.out.println("a person"); |
| } |
| |
| |
| if(e instanceof Graduate){ |
| System.out.println("a graduated student"); |
| } |
| if(e instanceof Student){ |
| System.out.println("a student"); |
| } |
| if(e instanceof Person){ |
| System.out.println("a person"); |
| } |
| } |
| } |
| |
3、练习3
GeometricObject类
| |
| |
| |
| public class GeometricObject { |
| protected String color; |
| protected double weight; |
| public String getColor() { |
| return color; |
| } |
| public void setColor(String color) { |
| this.color = color; |
| } |
| public double getWeight() { |
| return weight; |
| } |
| public void setWeight(double weight) { |
| this.weight = weight; |
| } |
| public GeometricObject(String color, double weight) { |
| super(); |
| this.color = color; |
| this.weight = weight; |
| } |
| |
| public double findArea(){ |
| return 0.0; |
| } |
| } |
| |
Circle类
| public class Circle extends GeometricObject { |
| |
| private double radius; |
| |
| public Circle(double weight,String color, double radius) { |
| super(color,weight); |
| this.radius = radius; |
| } |
| |
| public double getRadius() { |
| return radius; |
| } |
| |
| public void setRadius(double radius) { |
| this.radius = radius; |
| } |
| |
| @Override |
| public double findArea() { |
| return 3.14 * radius * radius; |
| } |
| } |
| |
MyRectangle类
| public class MyRectangle extends GeometricObject { |
| |
| private double width; |
| private double height; |
| |
| public MyRectangle(double width, double height,String color,double weight) { |
| super(color, weight); |
| this.height = height; |
| this.width = width; |
| } |
| |
| public double getWidth() { |
| return width; |
| } |
| |
| public void setWidth(double width) { |
| this.width = width; |
| } |
| |
| public double getHeight() { |
| return height; |
| } |
| |
| public void setHeight(double height) { |
| this.height = height; |
| } |
| |
| @Override |
| public double findArea() { |
| return width * height; |
| } |
| } |
| |
GeometricTest类
| |
| |
| |
| |
| |
| public class GeometricTest { |
| |
| public static void main(String[] args) { |
| GeometricTest test = new GeometricTest(); |
| |
| Circle c1 = new Circle(2.3,"white",1.0); |
| test.displayGeometricObject(c1); |
| |
| Circle c2 = new Circle(3.3,"white",1.0); |
| test.displayGeometricObject(c2); |
| |
| boolean isEqual = test.equalsArea(c1, c2); |
| System.out.println("面积是否相等: " + isEqual); |
| |
| MyRectangle rect = new MyRectangle(2.1, 3.4,"red",1.0); |
| test.displayGeometricObject(rect); |
| } |
| |
| public void displayGeometricObject(GeometricObject o){ |
| System.out.println("面积为: " + o.findArea()); |
| } |
| |
| |
| public boolean equalsArea(GeometricObject o1,GeometricObject o2){ |
| return o1.findArea() == o2.findArea(); |
| } |
| } |
| |
练习4
| |
| |
| |
| |
| |
| import java.util.Random; |
| |
| class Animal { |
| |
| protected void eat() { |
| System.out.println("animal eat food"); |
| } |
| } |
| |
| class Cat extends Animal { |
| |
| protected void eat() { |
| System.out.println("cat eat fish"); |
| } |
| } |
| |
| class Dog extends Animal { |
| |
| public void eat() { |
| System.out.println("Dog eat bone"); |
| } |
| } |
| |
| class Sheep extends Animal { |
| |
| public void eat() { |
| System.out.println("Sheep eat grass"); |
| |
| } |
| |
| } |
| |
| public class InterviewTest { |
| |
| public static Animal getInstance(int key) { |
| switch (key) { |
| case 0: |
| return new Cat (); |
| case 1: |
| return new Dog (); |
| default: |
| return new Sheep (); |
| } |
| |
| } |
| |
| public static void main(String[] args) { |
| int key = new Random().nextInt(3); |
| |
| System.out.println(key); |
| |
| Animal animal = getInstance(key); |
| |
| animal.eat(); |
| |
| } |
| } |
| |
4、面试题拓展
| |
| |
| |
| |
| |
| public class InterviewTest1 { |
| |
| public static void main(String[] args) { |
| Base base = new Sub(); |
| base.add(1, 2, 3); |
| |
| |
| |
| } |
| } |
| |
| class Base { |
| public void add(int a, int... arr) { |
| System.out.println("base"); |
| } |
| } |
| |
| class Sub extends Base { |
| |
| public void add(int a, int[] arr) { |
| System.out.println("sub_1"); |
| } |
| |
| |
| |
| |
| |
| } |
| |
07、Object 类的使用
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public class ObjectTest { |
| |
| public static void main(String[] args) { |
| |
| } |
| } |
| |
| class Order{ |
| |
| } |
| |
7.1、Object类中的主要结构

7.2、==操作符与equals方法
| import java.sql.Date; |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public class EqualsTest { |
| public static void main(String[] args) { |
| |
| |
| int i = 10; |
| int j = 10; |
| double d = 10.0; |
| System.out.println(i == j); |
| System.out.println(i == d); |
| |
| |
| |
| |
| char c = 10; |
| System.out.println(i == c); |
| |
| char c1 = 'A'; |
| char c2 = 65; |
| System.out.println(c1 == c2); |
| |
| |
| Customer cust1 = new Customer("Tom" ,21); |
| Customer cust2 = new Customer("Tom" ,21); |
| System.out.println(cust1 == cust2); |
| |
| String str1 = new String("BAT"); |
| String str2 = new String("BAT"); |
| System.out.println(str1 == str2); |
| System.out.println("*************************"); |
| System.out.println(cust1.equals(cust2)); |
| System.out.println(str1.equals(str2)); |
| |
| Date date1 = new Date(23432525324L); |
| Date date2 = new Date(23432525324L); |
| System.out.println(date1.equals(date2)); |
| } |
| } |
| |
Customer类
| public class Customer { |
| |
| private String name; |
| private int age; |
| 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 Customer() { |
| super(); |
| } |
| public Customer(String name, int age) { |
| super(); |
| this.name = name; |
| this.age = age; |
| } |
| |
| |
| |
| @Override |
| public boolean equals(Object obj) { |
| if (this == obj) |
| return true; |
| if (obj == null) |
| return false; |
| if (getClass() != obj.getClass()) |
| return false; |
| Customer other = (Customer) obj; |
| if (age != other.age) |
| return false; |
| if (name == null) { |
| if (other.name != null) |
| return false; |
| } else if (!name.equals(other.name)) |
| return false; |
| return true; |
| } |
| |
| |
| |
| |
| |
| |
| System.out.println("Customer equals()...."); |
| |
| |
| |
| |
| |
| |
| |
| if(this.age == cust.age && this.name.equals(cust.name)){ |
| return true; |
| }else{ |
| return false; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| } |
| |
7.2.1、重写equals()方法的原则
- 对称性:如果x.equals(y)返回是“true”,那么y.equals(x)也应该返回是“true”。
- 自反性:x.equals(x)必须返回是“true”。
- 传递性:如果x.equals(y)返回是“true”,而且y.equals(z)返回是“true”,那么z.equals(x)也应该返回是“true”。
- 一致性:如果x.equals(y)返回是“true”,只要x和y内容一直不变,不管你重复x.equals(y)多少次,返回都是“true”。
- 任何情况下,x.equals(null),永远返回是“false”;x.equals(和x不同类型的对象)永远返回是“false”。
| int it = 65; |
| float fl= 65.0f; |
| System.out.println("65和65.0f是否相等?" + (it == fl)); |
| char ch1 = 'A'; |
| char ch2 = 12; |
| System.out.println("65和'A'是否相等?" + (it == ch1)); |
| System.out.println("12和ch2是否相等?" + (12 == ch2)); |
| String str1 = new String("hello"); |
| String str2 = new String("hello"); |
| System.out.println("str1和str2是否相等?"+ (str1 == str2)); |
| System.out.println("str1是否equals str2?"+(str1.equals(str2))); |
| System.out.println("hello" == new java.util.Date()); |
| |
练习一
| |
| |
| |
| |
| |
| |
| |
| public class OrderTest { |
| public static void main(String[] args) { |
| Order order1 = new Order(1001,"AA"); |
| Order order2 = new Order(1001,"BB"); |
| |
| System.out.println(order1.equals(order2)); |
| |
| Order order3 = new Order(1001,"BB"); |
| System.out.println(order2.equals(order3)); |
| } |
| } |
| |
| class Order{ |
| private int orderId; |
| private String orderName; |
| public int getOrderId() { |
| return orderId; |
| } |
| public void setOrderId(int orderId) { |
| this.orderId = orderId; |
| } |
| public String getOrderName() { |
| return orderName; |
| } |
| public void setOrderName(String orderName) { |
| this.orderName = orderName; |
| } |
| public Order(int orderId, String orderName) { |
| super(); |
| this.orderId = orderId; |
| this.orderName = orderName; |
| } |
| public boolean equals(Object obj){ |
| if(this == obj){ |
| return true; |
| } |
| if(obj instanceof Order){ |
| Order order = (Order)obj; |
| |
| return this.orderId == order.orderId && this.orderName.equals(order.orderName); |
| |
| |
| } |
| return false; |
| } |
| } |
| |
练习二
| |
| |
| |
| |
| |
| public class MyDateTest { |
| public static void main(String[] args) { |
| MyDate m1= new MyDate(14, 3, 1976); |
| MyDate m2= new MyDate(14, 3, 1976); |
| if(m1== m2) { |
| System.out.println("m1==m2"); |
| } else{ |
| System.out.println("m1!=m2"); |
| } |
| |
| if(m1.equals(m2)) { |
| System.out.println("m1 is equal to m2"); |
| } else{ |
| System.out.println("m1 is not equal to m2"); |
| } |
| } |
| } |
| |
| class MyDate{ |
| private int day; |
| private int month; |
| private int year; |
| |
| public MyDate(int day, int month, int year) { |
| super(); |
| this.day = day; |
| this.month = month; |
| this.year = year; |
| } |
| |
| public int getDay() { |
| return day; |
| } |
| |
| public void setDay(int day) { |
| this.day = day; |
| } |
| |
| public int getMonth() { |
| return month; |
| } |
| |
| |
| public void setMonth(int month) { |
| this.month = month; |
| } |
| |
| public int getYear() { |
| return year; |
| } |
| |
| public void setYear(int year) { |
| this.year = year; |
| } |
| |
| @Override |
| public boolean equals(Object obj) { |
| if(this == obj){ |
| return true; |
| } |
| if(obj instanceof MyDate){ |
| MyDate myDate = (MyDate)obj; |
| return this.day == myDate.day && this.month == myDate.month && |
| this.year == myDate.year; |
| } |
| return false; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| } |
| |
7.3、toString的使用
| public class Customer { |
| |
| private String name; |
| private int age; |
| 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 Customer() { |
| super(); |
| } |
| public Customer(String name, int age) { |
| super(); |
| this.name = name; |
| this.age = age; |
| } |
| |
| |
| @Override |
| public boolean equals(Object obj) { |
| if (this == obj) |
| return true; |
| if (obj == null) |
| return false; |
| if (getClass() != obj.getClass()) |
| return false; |
| Customer other = (Customer) obj; |
| if (age != other.age) |
| return false; |
| if (name == null) { |
| if (other.name != null) |
| return false; |
| } else if (!name.equals(other.name)) |
| return false; |
| return true; |
| } |
| |
| |
| |
| |
| |
| |
| System.out.println("Customer equals()...."); |
| |
| |
| |
| |
| |
| |
| |
| if(this.age == cust.age && this.name.equals(cust.name)){ |
| return true; |
| }else{ |
| return false; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| @Override |
| public String toString() { |
| return "Customer [name=" + name + ", age=" + age + "]"; |
| } |
| |
| } |
| |
ToStringTest类
| import java.util.Date; |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public class ToStringTest { |
| public static void main(String[] args) { |
| |
| Customer cust1 = new Customer("Tom" ,21); |
| System.out.println(cust1.toString()); |
| System.out.println(cust1); |
| |
| String str = new String("MM"); |
| System.out.println(str); |
| |
| Date date = new Date(45362348664663L); |
| System.out.println(date.toString()); |
| |
| } |
| } |
| |
练习
GeometricObject类
| public class GeometricObject { |
| protected String color; |
| protected double weight; |
| |
| public GeometricObject() { |
| super(); |
| this.color = "white"; |
| this.weight = 1.0; |
| } |
| |
| public GeometricObject(String color, double weight) { |
| super(); |
| this.color = color; |
| this.weight = weight; |
| } |
| |
| public String getColor() { |
| return color; |
| } |
| |
| public void setColor(String color) { |
| this.color = color; |
| } |
| |
| public double getWeight() { |
| return weight; |
| } |
| |
| public void setWeight(double weight) { |
| this.weight = weight; |
| } |
| } |
| |
Circle类
| public class Circle extends GeometricObject{ |
| private double radius; |
| |
| public Circle() { |
| super(); |
| |
| |
| this.radius = 1.0; |
| } |
| |
| |
| public Circle(double radius) { |
| super(); |
| |
| |
| this.radius = radius; |
| } |
| |
| public Circle(double radius,String color,double weight) { |
| super(color,weight); |
| this.radius = radius; |
| } |
| |
| public double getRadius() { |
| return radius; |
| } |
| |
| public void setRadius(double radius) { |
| this.radius = radius; |
| } |
| |
| |
| public double findArea(){ |
| return Math.PI * radius * radius; |
| } |
| |
| @Override |
| public boolean equals(Object obj) { |
| if(this == obj){ |
| return true; |
| } |
| |
| if(obj instanceof Circle){ |
| Circle c = (Circle)obj; |
| return this.radius == c.radius; |
| } |
| |
| return false; |
| } |
| |
| @Override |
| public String toString() { |
| return "Circle [radius=" + radius + "]"; |
| } |
| |
| } |
| |
测试类
| |
| |
| |
| |
| |
| public class CircleTest { |
| public static void main(String[] args) { |
| |
| Circle circle1 = new Circle(2.3); |
| Circle circle2 = new Circle(3.3,"white",2.0); |
| |
| System.out.println("颜色是否相等: " + circle1.getColor().equals(circle2.color)); |
| |
| System.out.println("半径是否相等: " + circle1.equals(circle2)); |
| |
| System.out.println(circle1); |
| System.out.println(circle2.toString()); |
| } |
| } |
| |
08、包装类(Wrapper)的使用
8.1、单元测试方法的使用
| import java.util.Date; |
| import org.junit.Test; |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public class JUnit { |
| |
| int num = 10; |
| |
| |
| @Test |
| public void testEquals(){ |
| String s1 = "MM"; |
| String s2 = "MM"; |
| System.out.println(s1.equals(s2)); |
| |
| |
| |
| |
| |
| System.out.println(num); |
| show(); |
| } |
| |
| public void show(){ |
| num = 20; |
| System.out.println("show()..."); |
| } |
| |
| |
| @Test |
| public void testToString(){ |
| String s2 = "MM"; |
| System.out.println(s2.toString()); |
| } |
| } |
| |


8.2、包装类的使用
8.3、包装类与基本数据类型相互转换

| import org.junit.Test; |
| |
| |
| |
| |
| public class WrapperTest { |
| |
| |
| @Test |
| public void test5(){ |
| String str1 = "123"; |
| |
| |
| |
| |
| |
| |
| int num2 = Integer.parseInt(str1); |
| System.out.println(num2 + 1); |
| |
| String str2 = "true"; |
| Boolean b1 = Boolean.parseBoolean(str2); |
| System.out.println(b1); |
| |
| } |
| |
| |
| @Test |
| public void test4(){ |
| int num1 = 10; |
| |
| String str1 = num1 + ""; |
| |
| float f1 = 12.3f; |
| String str2 = String.valueOf(f1); |
| |
| Double d1 = new Double(12.4); |
| String str3 = String.valueOf(d1); |
| System.out.println(str2); |
| System.out.println(str3); |
| |
| } |
| |
| |
| |
| |
| @Test |
| public void test3(){ |
| |
| |
| |
| |
| |
| int num2 = 10; |
| Integer in1 = num2; |
| |
| boolean b1 = true; |
| Boolean b2 = b1; |
| |
| |
| System.out.println(in1.toString()); |
| |
| int num3 = in1; |
| |
| } |
| |
| public void method(Object obj){ |
| System.out.println(obj); |
| } |
| |
| |
| @Test |
| public void test2() { |
| Integer in1 = new Integer(12); |
| int i1 = in1.intValue(); |
| System.out.println(i1 + 1); |
| |
| Float f1 = new Float(12.3f); |
| float f2 = f1.floatValue(); |
| System.out.println(f2 + 1); |
| } |
| |
| |
| @Test |
| public void test1() { |
| int num1 = 10; |
| |
| |
| Integer in1 = new Integer(num1); |
| System.out.println(in1.toString()); |
| |
| Integer in2 = new Integer("123"); |
| System.out.println(in2.toString()); |
| |
| |
| |
| |
| |
| Float f1 = new Float(12.3f); |
| Float f2 = new Float("12.3"); |
| System.out.println(f1); |
| System.out.println(f2); |
| |
| Boolean b1 = new Boolean(true); |
| Boolean b2 = new Boolean("true"); |
| |
| Boolean b3 = new Boolean("true123"); |
| System.out.println(b3); |
| |
| Order order = new Order(); |
| System.out.println(order.isMale); |
| System.out.println(order.isFemale); |
| |
| } |
| } |
| |
| class Order{ |
| |
| boolean isMale; |
| Boolean isFemale; |
| } |
| |
8.4、练习
1、面试题
| import org.junit.Test; |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public class InterViewTest { |
| |
| @Test |
| public void test(){ |
| Object o1= true? new Integer(1) : new Double(2.0); |
| System.out.println(o1); |
| } |
| |
| @Test |
| public void test2(){ |
| Object o2; |
| if(true) |
| o2 = new Integer(1); |
| else |
| o2 = new Double(2.0); |
| System.out.println(o2); |
| } |
| |
| @Test |
| public void method1() { |
| Integer i = new Integer(1); |
| Integer j = new Integer(1); |
| System.out.println(i == j); |
| |
| |
| |
| |
| |
| Integer m = 1; |
| Integer n = 1; |
| System.out.println(m == n); |
| |
| Integer x = 128; |
| Integer y = 128; |
| System.out.println(x == y); |
| |
| } |
| } |
| |
2、编程题

| import java.util.Scanner; |
| import java.util.Vector; |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public class VectorTest { |
| public static void main(String[] args) { |
| |
| Scanner scan = new Scanner(System.in); |
| |
| |
| Vector v = new Vector(); |
| |
| |
| int maxScore = 0; |
| for (;;) { |
| System.out.println("请输入学生成绩(以负数代表输入结束)"); |
| int score = scan.nextInt(); |
| |
| if (score < 0) { |
| break; |
| } |
| if (score > 100) { |
| System.out.println("输入的数据非法,请重新输入"); |
| continue; |
| } |
| |
| |
| |
| |
| |
| v.addElement(score); |
| |
| if (maxScore < score) { |
| maxScore = score; |
| } |
| } |
| |
| |
| char level; |
| for (int i = 0; i < v.size(); i++) { |
| Object obj = v.elementAt(i); |
| |
| |
| |
| |
| int score = (int) obj; |
| |
| if (maxScore - score <= 10) { |
| level = 'A'; |
| } else if (maxScore - score <= 20) { |
| level = 'B'; |
| } else if (maxScore - score <= 30) { |
| level = 'C'; |
| } else { |
| level = 'D'; |
| } |
| |
| System.out.println("student-" + i + " score is " + score + ",level is " + level); |
| |
| } |
| } |
| } |
| |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!