Java学习(三)– 面向对象中级
https://www.bilibili.com/video/BV1fh411y7R8/?spm_id_from=333.999.0.0&vd_source=594d36a0860080a36fe599e0b84e5fb2
由于从这里开始学习了继承,
为了避免多种多样的类混在一起影响复习效率
现在规定每一段演示代码会分成好几个代码块, 如果没有特殊标明, 连续的几个代码块表示一段演示
特殊标明: 像练习一, 练习二这种明确标志, 就是为了给不同的演示做分割
连续代码块的顺序为: 父类 -> 子类 -> 入口类
所以每段演示的题目 (要求) 一般都在 入口类 中, 这一点要特别注意
以免在复习的时候不知道自己在看啥
一、包
1.1 作用
- 区分相同名字的类
- 当类很多时可以很方便地管理类 (比如 Java API 文档)
- 控制访问范围
1.2 基本语法
package 包名
package 表示关键字打包
1.3 包的本质
本质就是创建不同的 文件夹/目录 保存类文件

1.4 包的命名规则
- 只能包含字母, 数字, 下划线, 小圆点
- 数字不能用作开头
- 不能是关键字或者保留字
- 一般是小写字母 + 小圆点
com.公司名.项目名.业务模块名
1.5 常用包
java.lang.* //lang 包是基本包,默认引入,不需要再引入.
java.util.* //util 包,系统提供的工具包, 工具类,使用 Scanner
java.net.* //网络包,网络开发
java.awt.* //是做 java 的界面开发,GUI
1.6 导入包
import 包名
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| package com.hspedu.pkg;
import java.util.Arrays;
public class Import01 {
public static void main(String[] args) {
int[] arr = {-1, 20, 2, 13, 3}; Arrays.sort(arr); for (int i = 0; i < arr.length ; i++) { System.out.print(arr[i] + "\t"); } } }
|
1.7 使用细节
- package 的作用是声明当前类所在的包, 需要放在类的最上面, 一个类中最多只有一个 package
- import 指令放在 package 的下面, 在类定义的前面, 可以有很多句, 没有顺序要求
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
package com.hspedu.pkg;
import java.util.Scanner; import java.util.Arrays;
public class PkgDetail {
public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int[] arr = {0, -1, 1}; Arrays.sort(args); } }
|
二、访问修饰符
公开级别: 用 public 修饰, 对外公开
受保护级别: 用 protected 修饰, 对子类和同一个包中的类公开
默认级别: 没有修饰符号, 向同一个包的类公开.
私有级别: 用 private 修饰, 只有类本身可以访问,不对外公开.

- 修饰符可以用来修饰类中的属性, 成员方法, 类
- 只有 默认和
public 才可以修饰类, 并遵循上述访问权限的特点
- 成员方法的访问规则和属性完全一样
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| package com.hspedu.modifier;
public class A {
public int n1 = 100; protected int n2 = 200; int n3 = 300; private int n4 = 400; public void m1() { System.out.println("n1=" + n1 + " n2=" + n2 + " n3=" + n3 + " n4=" + n4); } protected void m2() { } void m3() { } private void m4() { } public void hi() { m1(); m2(); m3(); m4(); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| package com.hspedu.modifier;
public class B { public void say() { A a = new A(); System.out.println("n1=" + a.n1 + " n2=" + a.n2 + " n3=" + a.n3 );
a.m1(); a.m2(); a.m3(); }
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| package com.hspedu.modifier;
public class Test { public static void main(String[] args) { A a = new A (); a.m1(); B b = new B(); b.say(); } }
class Tiger{ }
|
三、封装
3.1 介绍
封装 (encapsulation) 就是把抽象出的数据 [属性] 和对数据的操作 [方法] 封装在一起,数据被保护在内部,程序的其它部分只有通过被授权的操作 [方法] , 才能对数据进行操作。
3.2 好处
- 可以隐藏实现细节
- 可以对数据进行验证, 保证安全合理
3.3 如何实现
- 将属性进行私有化
private (不能直接修改属性)
- 提供一个公共的 (public)
set 方法, 用来对属性判断赋值 setter
- 提供一个公共的 (public)
get 方法, 用来获取属性的值 getter
- 还可以将
setter 与构造器结合, 每次调用构造器的时候里面全都是 setter 方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
| package com.hspedu.encap;
public class Encapsulation01 {
public static void main(String[] args) { Person person = new Person(); person.setName("韩顺平"); person.setAge(30); person.setSalary(30000); System.out.println(person.info()); System.out.println(person.getSalary());
Person smith = new Person("smith", 80, 50000); System.out.println("====smith的信息======"); System.out.println(smith.info());
} }
class Person { public String name; private int age; private double salary;
public void say(int n,String name) {
} public Person() { } public Person(String name, int age, double salary) {
setName(name); setAge(age); setSalary(salary); }
public String getName() { return name; } public void setName(String name) { if(name.length() >= 2 && name.length() <=6 ) { this.name = name; }else { System.out.println("名字的长度不对,需要(2-6)个字符,默认名字"); this.name = "无名人"; } }
public int getAge() { return age; }
public void setAge(int age) { if(age >= 1 && age <= 120) { this.age = age; } else { System.out.println("你设置年龄不对,需要在 (1-120), 给默认年龄18 "); this.age = 18; } }
public double getSalary() { return salary; }
public void setSalary(double salary) { this.salary = salary; } public String info() { return "信息为 name=" + name + " age=" + age + " 薪水=" + salary; } }
|
3.4 练习
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
| package com.hspedu.encap;
public class Account { private String name; private double balance; private String pwd;
public Account() { }
public Account(String name, double balance, String pwd) { this.setName(name); this.setBalance(balance); this.setPwd(pwd); }
public String getName() { return name; }
public void setName(String name) { if (name.length() >= 2 && name.length() <= 4) { this.name = name; } else { System.out.println("姓名要求(长度为2位3位或4位),默认值 无名"); this.name = "无名"; } }
public double getBalance() { return balance; }
public void setBalance(double balance) { if (balance > 20) { this.balance = balance; } else { System.out.println("余额(必须>20) 默认为0"); } }
public String getPwd() { return pwd; }
public void setPwd(String pwd) { if (pwd.length() == 6) { this.pwd = pwd; } else { System.out.println("密码(必须是六位)默认密码为 000000"); this.pwd = "000000"; } } public void showInfo() { System.out.println("账号信息 name=" + name + " 余额=" + balance + " 密码" + pwd);
} }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| package com.hspedu.encap;
public class TestAccount { public static void main(String[] args) { Account account = new Account(); account.setName("jack"); account.setBalance(60); account.setPwd("123456");
account.showInfo(); } }
|
四、继承
继承可以解决代码复用, 让我们的编程更加靠近人类思维. 当多个类存在相同的属性 (变量) 和 方法 时, 可以从这些类中抽象出父类, 在父类中定义这些相同的 属性 和 方法,所有的子类不需要重新定义这些属性和方法,只需要通过 extends 来声明继承父类即可。

4.2 继承的基本语法
class 子类 extends 父类 {
}
- 子类会自动拥有父类定义的属性和方法
- 父类又叫 超类, 基类
- 子类又叫 派生类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| package com.hspedu.extend_.improve_;
import com.hspedu.extend_.Graduate; import com.hspedu.extend_.Pupil;
public class Extends01 { public static void main(String[] args) { com.hspedu.extend_.Pupil pupil = new Pupil(); pupil.name = "银角大王~"; pupil.age = 11; pupil.testing(); pupil.setScore(50); pupil.showInfo();
System.out.println("======="); com.hspedu.extend_.Graduate graduate = new Graduate(); graduate.name = "金角大王~"; graduate.age = 23; graduate.testing(); graduate.setScore(80); graduate.showInfo(); } }
|
1 2 3 4 5 6 7 8 9
| package com.hspedu.extend_.improve_;
public class Graduate extends Student {
public void testing() { System.out.println("大学生 " + name + " 正在考大学数学.."); } }
|
1 2 3 4 5 6 7 8 9
| package com.hspedu.extend_.improve_;
public class Pupil extends Student { public void testing() { System.out.println("小学生 " + name + " 正在考小学数学.."); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| package com.hspedu.extend_.improve_;
public class Student { public String name; public int age; private double score; public void setScore(double score) { this.score = score; }
public void showInfo() { System.out.println("学生名 " + name + " 年龄 " + age + " 成绩 " + score); } }
|
4.3 好处
- 代码复用性提高
- 代码的扩展性和维护性提高
4.4 使用细节
子类继承了所有的属性和方法,非私有的属性和方法可以在子类直接访问, 但是私有属性和方法不能在子类直接访问,要通过父类提供公共的方法去访问
子类必须调用父类的构造器, 完成父类的初始化 super
当创建子类对象时,不管使用子类的哪个构造器,默认情况下总会去调用父类的无参构造器,如果父类没有提供无参构造器,则必须在子类的构造器中用 super 去指定使用父类的哪个构造器完成对父类的初始化工作,否则,编译不会通过(怎么理解。) [举例说明]
如果希望指定去调用父类的某个构造器,则显式的调用一下 : super (参数列表)
super 在使用时,必须放在构造器第一行 (super 只能在构造器中使用)
super() 和 this() 都只能放在构造器第一行,因此这两个方法不能共存在一个构造器
注意 不管怎么样子类都会通过某一个方法调用 super 的
java 所有类都是 Object 类的子类, Object 是所有类的基类.
父类构造器的调用不限于直接父类!将一直往上追溯直到 Object 类 (顶级父类)
子类最多只能继承一个父类 (指直接继承),即 java 中是单继承机制。思考:如何让 A 类继承 B 类和 C 类? 【A 继承 B, B 继承 C】
不能滥用继承,子类和父类之间必须满足 is-a 的逻辑关系
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| package com.hspedu.extend_;
public class ExtendsDetail { public static void main(String[] args) {
System.out.println("===第3对象===="); Sub sub3 = new Sub("king", 10); } }
|
1 2 3 4 5 6 7 8 9 10
| package com.hspedu.extend_;
public class TopBase {
public TopBase() { System.out.println("构造器TopBase() 被调用..."); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| package com.hspedu.extend_;
public class Base extends TopBase { public int n1 = 100; protected int n2 = 200; int n3 = 300; private int n4 = 400;
public Base() { System.out.println("父类Base()构造器被调用...."); } public Base(String name, int age) { System.out.println("父类Base(String name, int age)构造器被调用...."); } public Base(String name) { System.out.println("父类Base(String name)构造器被调用...."); } public int getN4() { return n4; } public void test100() { System.out.println("test100"); } protected void test200() { System.out.println("test200"); } void test300() { System.out.println("test300"); } private void test400() { System.out.println("test400"); } public void callTest400() { test400(); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| package com.hspedu.extend_;
import java.util.Arrays;
public class Sub extends Base {
public Sub(String name, int age) { super("king", 20);
System.out.println("子类Sub(String name, int age)构造器被调用....");
}
public Sub() { super("smith", 10); System.out.println("子类Sub()构造器被调用...."); } public Sub(String name) { super("tom", 30); System.out.println("子类Sub(String name)构造器被调用...."); }
public void sayOk() { System.out.println(n1 + " " + n2 + " " + n3); test100(); test200(); test300(); System.out.println("n4=" + getN4()); callTest400(); }
}
|
4.5 继承的本质 (※)
- 现在方法区中加载类, 并识别类之间的继承关系
- 然后先从 最顶级的父类开始加载
Object -> GrandPa -> Father -> Son , 将他们的属性及方法加载出来
- 先加载 GrandPa 类的两个属性, 再加载 Father 类的两个属性, 再加载 Son 类的属性
- 调用属性时遵循就近原则, 如果子类没有就向上找, 没有就一直向上找, 如果没有或者无访问权限就报错
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| package com.hspedu.extend_;
public class ExtendsTheory { public static void main(String[] args) { Son son = new Son(); System.out.println(son.name); System.out.println(son.hobby); } }
class GrandPa { String name = "大头爷爷"; String hobby = "旅游"; }
class Father extends GrandPa { String name = "大头爸爸"; private int age = 39;
public int getAge() { return age; } }
class Son extends Father { String name = "大头儿子"; }
|

4.6 练习
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| package com.hspedu.extend_.exercise;
public class ExtendsExercise02 { public static void main(String[] args) { C c = new C(); } }
class A {
public A() { System.out.println("我是A类"); } }
class B extends A { public B() { System.out.println("我是B类的无参构造"); }
public B(String name) { System.out.println(name + "我是B类的有参构造"); } }
class C extends B { public C() { this("hello"); System.out.println("我是c类的无参构造"); }
public C(String name) { super("hahah"); System.out.println("我是c类的有参构造"); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| package com.hspedu.extend_.exercise;
public class ExtendsExercise03 { public static void main(String[] args) { PC pc = new PC("intel", 16, 500, "IBM"); pc.printInfo(); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| package com.hspedu.extend_.exercise;
public class PC extends Computer{
private String brand; public PC(String cpu, int memory, int disk, String brand) { super(cpu, memory, disk); this.brand = brand; } public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } public void printInfo() { System.out.println("PC信息=");
System.out.println(getDetails() + " brand=" + brand); }
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| package com.hspedu.extend_.exercise;
public class Computer { private String cpu; private int memory; private int disk; public Computer(String cpu, int memory, int disk) { this.cpu = cpu; this.memory = memory; this.disk = disk; } public String getDetails() { return "cpu=" + cpu + " memory=" + memory + " disk=" + disk; }
public String getCpu() { return cpu; }
public void setCpu(String cpu) { this.cpu = cpu; }
public int getMemory() { return memory; }
public void setMemory(int memory) { this.memory = memory; }
public int getDisk() { return disk; }
public void setDisk(int disk) { this.disk = disk; } }
|
五、super
super 代表父类的引用,用于访问父类的属性、方法、构造器
5.1 基本语法
- 访问父类的属性 (不能访问父类的 private 属性)
super.属性名
- 访问父类的方法 (不能访问父类的 private 方法)
super.方法名(参数列表)
- 访问父类的构造器
super(参数列表); 只能放在构造器的第一句l,只能出现一句
1 2 3 4 5 6 7 8 9 10 11
| package com.hspedu.super_;
public class Super01 { public static void main(String[] args) { B b = new B(); b.test(); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| package com.hspedu.super_;
public class Base {
public int n1 = 999; public int age = 111; public void cal() { System.out.println("Base类的cal() 方法..."); } public void eat() { System.out.println("Base类的eat()....."); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| package com.hspedu.super_;
public class A extends Base{ protected int n2 = 200; int n3 = 300; private int n4 = 400;
public A() {} public A(String name) {} public A(String name, int age) {}
public void test100() { }
protected void test200() { }
void test300() { }
private void test400() { } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| package com.hspedu.super_;
public class B extends A {
public int n1 = 888;
public void test() {
System.out.println("super.n1=" + super.n1); super.cal(); }
public void hi() { System.out.println(super.n1 + " " + super.n2 + " " + super.n3 ); } public void cal() { System.out.println("B类的cal() 方法..."); } public void sum() { System.out.println("B类的sum()");
this.cal();
System.out.println(n1); System.out.println(this.n1);
System.out.println(super.n1);
} public void ok() { super.test100(); super.test200(); super.test300(); } public B() { super("jack"); } }
|
5.2 好处
调用父类构造器, 分工明确, 父类属性由父类构造器初始化, 子类属性由子类构造器初始化
当子类中有和父类中的成员 (属性 或 方法) 重名时, 为了访问父类的成员, 必须通过 super
如果没有重名, 使用 super, this, 啥都不用 效果一样
super 的访问不限于直接父类, 如果爷爷类和本类中有同名的成员, 也可以使用 super 去访问爷爷类的成员, 如果多个基类 (上级类) 中都有同名的成员, 使用 super 访问遵循就近原则, 也遵守访问权限相关规则
5.3 super & this
super 跟 this 很像, 就是找属性或方法时直接跳过本类找父类

六、方法重写 (覆盖)
子类和父类的某个方法名称, 返回类型, 参数一样, 那么就说子类的这个方法重写 (覆盖) 了父类的方法 (属性没有重写一说)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| package com.hspedu.override_;
public class Animal { public void cry() { System.out.println("动物叫唤.."); }
public Object m1() { return null; }
public String m2() { return null; }
public AAA m3() { return null; } protected void eat() {
} }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| package com.hspedu.override_;
public class Dog extends Animal{ public void cry() { System.out.println("小狗汪汪叫.."); }
public String m1() { return null; }
public void eat() {
} }
class AAA {
}
class BBB extends AAA {
}
|
1 2 3 4 5 6 7 8 9 10 11
| package com.hspedu.override_;
public class Override01 { public static void main(String[] args) { Dog dog = new Dog(); dog.cry(); } }
|
6.1 使用细节
子类方法的形参列表, 方法名称, 要和父类方法的形参列表, 方法名称完全一样
子类方法的返回类型和父类方法的返回类型一样, 或者是父类返回类型的子类

子类方法不能缩小父类方法的访问权限 (可以一样或扩大)

6.2 重写 & 重载

6.3 练习
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| package com.hspedu.override_;
public class Person { private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } public String say() { return "name=" + name + " age=" + 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; } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| package com.hspedu.override_;
public class Student extends Person{ private int id; private double score;
public Student(String name, int age, int id, double score) { super(name, age); this.id = id; this.score = score; } public String say() { return super.say() + " id=" + id + " score=" + score; } public int getId() { return id; }
public void setId(int id) { this.id = id; }
public double getScore() { return score; }
public void setScore(double score) { this.score = score; } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| package com.hspedu.override_;
public class OverrideExercise { public static void main(String[] args) { Person jack = new Person("jack", 10); System.out.println(jack.say());
Student smith = new Student("smith", 20, 123456, 99.8); System.out.println(smith.say()); } }
|
七、多态
方法或对象具有多种形态
7.1 问题引出

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| package com.hspedu.poly_;
public class Animal { private String name;
public Animal(String name) { this.name = name; }
public String getName() { return name; }
public void setName(String name) { this.name = name; } }
|
1 2 3 4 5 6 7 8
| package com.hspedu.poly_;
public class Cat extends Animal { public Cat(String name) { super(name); } }
|
1 2 3 4 5 6 7 8
| package com.hspedu.poly_;
public class Dog extends Animal { public Dog(String name) { super(name); } }
|
1 2 3 4 5 6 7 8
| package com.hspedu.poly_;
public class Pig extends Animal { public Pig(String name) { super(name); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| package com.hspedu.poly_;
public class Food { private String name;
public Food(String name) { this.name = name; }
public String getName() { return name; }
public void setName(String name) { this.name = name; } }
|
1 2 3 4 5 6 7 8
| package com.hspedu.poly_;
public class Bone extends Food { public Bone(String name) { super(name); } }
|
1 2 3 4 5 6 7 8
| package com.hspedu.poly_;
public class Fish extends Food { public Fish(String name) { super(name); } }
|
1 2 3 4 5 6 7 8
| package com.hspedu.poly_;
public class Rice extends Food { public Rice(String name) { super(name); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| package com.hspedu.poly_;
public class Master { private String name;
public Master(String name) { this.name = name; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public void feed(Animal animal, Food food) { System.out.println("主人 " + name + " 给 " + animal.getName() + " 吃 " + food.getName()); }
}
|
使用多态机制解决这个问题
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| package com.hspedu.poly_;
public class Poly01 { public static void main(String[] args) {
Master tom = new Master("汤姆"); Dog dog = new Dog("大黄~"); Bone bone = new Bone("大棒骨~"); tom.feed(dog, bone);
Cat cat = new Cat("小花猫~"); Fish fish = new Fish("黄花鱼~"); System.out.println("===========-------"); tom.feed(cat, fish);
Pig pig = new Pig("小花猪"); Rice rice = new Rice("米饭"); System.out.println("==================="); tom.feed(pig, rice); } }
|
7.2 方法的多态
重写 和 重载就体现了多态
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| package com.hspedu.poly_;
public class PloyMethod { public static void main(String[] args) { A a = new A(); System.out.println(a.sum(10, 20)); System.out.println(a.sum(10, 20, 30));
B b = new B(); a.say(); b.say();
} } class B { public void say() { System.out.println("B say() 方法被调用..."); } } class A extends B { public int sum(int n1, int n2){ return n1 + n2; } public int sum(int n1, int n2, int n3){ return n1 + n2 + n3; }
public void say() { System.out.println("A say() 方法被调用..."); } }
|
7.3 对象的多态 (※)
- 一个对象的百年一类型和运行类型可以不一致
- 编译类型在定义对象时就确定了, 不能改变
- 运行类型是可变的
- 编译类型看定义时
= 左边, 运行类型看 = 右边
1 2 3 4 5 6 7 8
| package com.hspedu.poly_.objectpoly_;
public class Animal { public void cry() { System.out.println("Animal cry() 动物在叫...."); } }
|
1 2 3 4 5 6 7 8 9
| package com.hspedu.poly_.objectpoly_;
public class Cat extends Animal {
public void cry() { System.out.println("Cat cry() 小猫喵喵叫..."); } }
|
1 2 3 4 5 6 7 8 9
| package com.hspedu.poly_.objectpoly_;
public class Dog extends Animal {
public void cry() { System.out.println("Dog cry() 小狗汪汪叫..."); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| package com.hspedu.poly_.objectpoly_;
public class PolyObject { public static void main(String[] args) { Animal animal = new Dog(); animal.cry();
animal = new Cat(); animal.cry(); } }
|
7.4 注意事项
- 多态的前提是: 两个对象 (类) 之间存在着继承关系
7.4.1 向上转型
本质: 父类的引用指向了子类的对象 (子类向上转型成了父类)
语法: 父类类型 引用名 = new 子类类型();
特点:
编译类型看左边, 运行类型看右边
可以调用父类中的所有成员 (要遵守访问权限)
不能调用子类中的特有成员
最终运行效果要看子类的具体实现
子类和父类可能具有相同的方法, 此时进行向上转型
编译的指令是 javac, 编译通过的是父类的方法, 但是此时子类有与父类同名的方法 (重写)
运行的指令是 java, 与编译的指令无关, 哪个方法通过编译, 哪个方法就可以运行
则此时运行的方法其实是子类重写过后的与父类同名的方法
7.4.2 向下转型
语法: 子类类型 引用名 = (子类类型) 父类引用; (父类向下转型成了子类)
只能强转父类的引用, 不能强转父类的对象 (对象创建出来是啥就是啥, 不能强转)
要求父类的引用必须指向的是当前目标类型的对象
就是在这里要对父类的引用进行向下转型时, 要向下转型的这个子类姑且设为 A
而这个要向下转型的父类在向上转型时, 它当时指向的对象必须是 A 类的对象
否则不可以被向下转型
相当于一上一下转回来了, 目的就是使用子类的特有方法
当向下转型后, 可以调用子类类型中的所有成员
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| package com.hspedu.poly_.detail_;
public class Animal { String name = "动物"; int age = 10; public void sleep(){ System.out.println("睡"); } public void run(){ System.out.println("跑"); } public void eat(){ System.out.println("吃"); } public void show(){ System.out.println("hello,你好"); }
}
|
1 2 3 4 5 6 7 8 9 10 11
| package com.hspedu.poly_.detail_;
public class Cat extends Animal { public void eat(){ System.out.println("猫吃鱼"); } public void catchMouse(){ System.out.println("猫抓老鼠"); } }
|
1 2 3 4 5
| package com.hspedu.poly_.detail_;
public class Dog extends Animal { }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| package com.hspedu.poly_.detail_;
public class PolyDetail { public static void main(String[] args) {
Animal animal = new Cat(); Object obj = new Cat();
animal.eat(); animal.run(); animal.show(); animal.sleep();
Cat cat = (Cat) animal; cat.catchMouse(); Dog dog = (Dog) animal;
System.out.println("ok~~"); } }
|
编译的时候, 属性就已经确定了, 这跟向上转型思想差不多 (忘了就看看我在上面的分析笔记)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| package com.hspedu.poly_.detail_;
public class PolyDetail02 { public static void main(String[] args) { Base base = new Sub(); System.out.println(base.count); Sub sub = new Sub(); System.out.println(sub.count); } }
class Base { int count = 10; } class Sub extends Base { int count = 20; }
|
instanceOf 比较操作符,用于判断对象的运行类型是否为 XX 类型或 XX 类型的子类型
A instanceof B, A 是不是 B 的….
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| package com.hspedu.poly_.detail_;
public class PolyDetail03 { public static void main(String[] args) { BB bb = new BB(); System.out.println(bb instanceof BB); System.out.println(bb instanceof AA);
AA aa = new BB(); System.out.println(aa instanceof AA); System.out.println(aa instanceof BB);
Object obj = new Object(); System.out.println(obj instanceof AA); String str = "hello"; System.out.println(str instanceof Object); } }
class AA {} class BB extends AA {}
|
7.5 动态绑定机制
当调用对象方法时, 该方法会和该对象的内存地址/运行类型绑定
就是先在子类找方法, 没有的话就向上找, 在父类的方法里找, 一直到 Object, 没有的话报错
对比 继承 那部分的内存图
当调用对象属性时, 没有动态绑定机制, 哪里声明, 哪里使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| package com.hspedu.poly_.dynamic_;
public class DynamicBinding { public static void main(String[] args) { A a = new B(); System.out.println(a.sum()); System.out.println(a.sum1()); } }
class A { public int i = 10;
public int sum() { return getI() + 10; }
public int sum1() { return i + 10; }
public int getI() { return i; } }
class B extends A { public int i = 20;
public int getI() { return i; }
}
|
7.6 多态数组
数组的定义类型为父类类型,里面保存的实际元素类型为子类类型
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| package com.hspedu.poly_.polyarr_;
public class Person { private String name; private int age;
public Person(String name, int age) { this.name = name; this.age = 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 String say() { return name + "\t" + age; } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| package com.hspedu.poly_.polyarr_;
public class Student extends Person { private double score;
public Student(String name, int age, double score) { super(name, age); this.score = score; }
public double getScore() { return score; }
public void setScore(double score) { this.score = score; }
@Override public String say() { return "学生 " + super.say() + " score=" + score; } public void study() { System.out.println("学生 " + getName() + " 正在学java..."); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| package com.hspedu.poly_.polyarr_;
public class Teacher extends Person { private double salary;
public Teacher(String name, int age, double salary) { super(name, age); this.salary = salary; }
public double getSalary() { return salary; }
public void setSalary(double salary) { this.salary = salary; }
@Override public String say() { return "老师 " + super.say() + " salary=" + salary; } public void teach() { System.out.println("老师 " + getName() + " 正在讲java课程..."); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| package com.hspedu.poly_.polyarr_;
public class PloyArray { public static void main(String[] args) {
Person[] persons = new Person[5]; persons[0] = new Person("jack", 20); persons[1] = new Student("mary", 18, 100); persons[2] = new Student("smith", 19, 30.1); persons[3] = new Teacher("scott", 30, 20000); persons[4] = new Teacher("king", 50, 25000);
for (int i = 0; i < persons.length; i++) { System.out.println(persons[i].say()); if(persons[i] instanceof Student) { Student student = (Student)persons[i]; student.study(); } else if(persons[i] instanceof Teacher) { Teacher teacher = (Teacher)persons[i]; teacher.teach(); } else if(persons[i] instanceof Person){ } else { System.out.println("你的类型有误, 请自己检查..."); }
}
} }
|
7.7 多态参数
方法定义的形参类型为父类类型,实参类型允许为子类类型
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| package com.hspedu.poly_.polyparameter_;
public class Employee { private String name; private double salary;
public Employee(String name, double salary) { this.name = name; this.salary = salary; } public double getAnnual() { return 12 * salary; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public double getSalary() { return salary; }
public void setSalary(double salary) { this.salary = salary; } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| package com.hspedu.poly_.polyparameter_;
public class Manager extends Employee{
private double bonus;
public Manager(String name, double salary, double bonus) { super(name, salary); this.bonus = bonus; }
public double getBonus() { return bonus; }
public void setBonus(double bonus) { this.bonus = bonus; } public void manage() { System.out.println("经理 " + getName() + " is managing"); } @Override public double getAnnual() { return super.getAnnual() + bonus; } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| package com.hspedu.poly_.polyparameter_;
public class Worker extends Employee { public Worker(String name, double salary) { super(name, salary); } public void work() { System.out.println("普通员工 " + getName() + " is working"); }
@Override public double getAnnual() { return super.getAnnual(); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| package com.hspedu.poly_.polyparameter_;
public class PloyParameter { public static void main(String[] args) { Worker tom = new Worker("tom", 2500); Manager milan = new Manager("milan", 5000, 200000); PloyParameter ployParameter = new PloyParameter(); ployParameter.showEmpAnnual(tom); ployParameter.showEmpAnnual(milan);
ployParameter.testWork(tom); ployParameter.testWork(milan);
}
public void showEmpAnnual(Employee e) { System.out.println(e.getAnnual()); } public void testWork(Employee e) { if(e instanceof Worker) { ((Worker) e).work(); } else if(e instanceof Manager) { ((Manager) e).manage(); } else { System.out.println("不做处理..."); } } }
|
八、Object 类详解
8.1 equals 方法
8.1.1 == 和 equals 的对比 (面试)
==
- 既可以判断基本类型, 又可以判断引用类型
- 如果判断基本类型, 判断的是值是否相等
- 如果判断引用类型, 判断的是地址是否相等, 即判定是不是一个对象
equals
是 Object 类中的方法, 只能判断引用类型 (使用前记得查看 jdk 源码)
默认判断的是地址是否相等, 子类中往往重写该方法, 用于判断内容是否相等
比如 Integer, String (看源码)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
| package com.hspedu.object_;
public class Equals01 {
public static void main(String[] args) { A a = new A(); A b = a; A c = b; System.out.println(a == c); System.out.println(b == c); B bObj = a; System.out.println(bObj == c); int num1 = 10; double num2 = 10.0; System.out.println(num1 == num2);
"hello".equals("abc");
Integer integer1 = new Integer(1000); Integer integer2 = new Integer(1000); System.out.println(integer1 == integer2); System.out.println(integer1.equals(integer2));
String str1 = new String("hspedu"); String str2 = new String("hspedu"); System.out.println(str1 == str2); System.out.println(str1.equals(str2));
} }
class B {} class A extends B {}
|
8.1.2 如何重写 equals 方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| package com.hspedu.object_;
public class EqualsExercise01 { public static void main(String[] args) { Person person1 = new Person("jack", 10, '男'); Person person2 = new Person("jack", 20, '男');
System.out.println(person1.equals(person2)); } }
class Person{ private String name; private int age; private char gender;
public boolean equals(Object obj) { if(this == obj) { return true; } if(obj instanceof Person) {
Person p = (Person)obj; return this.name.equals(p.name) && this.age == p.age && this.gender == p.gender; } return false;
}
public Person(String name, int age, char gender) { this.name = name; this.age = age; this.gender = gender; }
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; }
}
|
8.1.3 练习
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| package com.hspedu.object_;
public class EqualsExercise02 { public static void main(String[] args) {
Person_ p1 = new Person_(); p1.name = "hspedu";
Person_ p2 = new Person_(); p2.name = "hspedu";
System.out.println(p1==p2); System.out.println(p1.name .equals( p2.name)); System.out.println(p1.equals(p2));
String s1 = new String("asdf");
String s2 = new String("asdf"); System.out.println(s1.equals(s2)); System.out.println(s1==s2);
} }
class Person_{ public String name; }
|
1 2 3 4 5 6 7 8 9 10 11 12
| 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.sql.Date());
|
8.2 hashCode方法
提高具有哈希结构的容器的效率!
两个引用,如果指向的是同一个对象,则哈希值肯定是一样的!
两个引用,如果指向的是不同对象,则哈希值是不一样的
哈希值主要根据地址号来的, 不能完全将哈希值等价于地址。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| package com.hspedu.object_;
public class HashCode_ { public static void main(String[] args) {
AA aa = new AA(); AA aa2 = new AA(); AA aa3 = aa; System.out.println("aa.hashCode()=" + aa.hashCode()); System.out.println("aa2.hashCode()=" + aa2.hashCode()); System.out.println("aa3.hashCode()=" + aa3.hashCode());
} } class AA {}
|
8.3 toString 方法
基本介绍
默认返回:全类名+@+哈希值的十六进制,【查看 Object 的 toString 方法】
子类往往重写 toString 方法,用于返回对象的属性信息
重写 toString 方法,打印对象或拼接对象时,都会自动调用该对象的 toString 形式.
当直接输出一个对象时,toString 方法会被默认的调用, 比如 System.out.println(monster); 就会默认调用 monster.toString()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| package com.hspedu.object_;
public class ToString_ { public static void main(String[] args) {
Monster monster = new Monster("小妖怪", "巡山的", 1000); System.out.println(monster.toString() + " hashcode=" + monster.hashCode());
System.out.println("==当直接输出一个对象时,toString 方法会被默认的调用=="); System.out.println(monster);
} }
class Monster { private String name; private String job; private double sal;
public Monster(String name, String job, double sal) { this.name = name; this.job = job; this.sal = sal; }
@Override public String toString() { return "Monster{" + "name='" + name + '\'' + ", job='" + job + '\'' + ", sal=" + sal + '}'; }
@Override protected void finalize() throws Throwable { System.out.println("fin.."); } }
|
8.4 finalize 方法
当对象被回收时,系统自动调用该对象的 finalize 方法。子类可以重写该方法,做一些释放资源的操作
什么时候被回收:当某个对象没有任何引用时,则 jvm 就认为这个对象是一个垃圾对象,就会使用垃圾回收机制来销毁该对象,在销毁该对象前,会先调用 finalize 方法。
垃圾回收机制的调用,是由系统来决定 (即有自己的 GC 算法), 也可以通过 System.gc() 主动触发垃圾回收机制
在实际开发中,几乎不会运用 finalize , 所以更多就是为了应付面试.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| package com.hspedu.object_;
public class Finalize_ { public static void main(String[] args) {
Car bmw = new Car("宝马"); bmw = null; System.gc();
System.out.println("程序退出了...."); } } class Car { private String name; public Car(String name) { this.name = name; } @Override protected void finalize() throws Throwable { System.out.println("我们销毁 汽车" + name ); System.out.println("释放了某些资源...");
} }
|
九、断点调试
在断点调试过程中, 是运行状态, 是以对象的 运行类型 来执行的
9.1 介绍
- 断点调试是指在程序的某一行设置- 个断点,调试时,程序运行到这一行就会停住,然后你可以一步-步往下调试,调试过程中可以看各个变量当前的值,出错的话,调试到出错的代码行即显示错误,停下。进行分析从而找到这个Bug
- 断点调试是程序员必须掌握的技能。
- 断点调试也能帮助我们查看java底层源代码的执行过程,提高程序员的Java水平。
9.2 快捷键
- F7( 跳入) 跳入方法内
- F8 (跳过) 逐行执行代码.
- shift+F8 (跳出) 跳出方法
- F9 (resume,执行到下一个断点)

9.3 应用案例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| package com.hspedu.debug_;
public class Debug01 { public static void main(String[] args) { int sum = 0; for (int i = 0; i < 5; i++) { sum += i; System.out.println("i=" + i); System.out.println("sum=" + i); } System.out.println("退出for...."); } }
|
1 2 3 4 5 6 7 8 9 10 11 12
| package com.hspedu.debug_;
public class Debug02 { public static void main(String[] args) { int[] arr = {1, 10, -1}; for (int i = 0; i <= arr.length; i++) { System.out.println(arr[i]); } System.out.println("退出for"); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| package com.hspedu.debug_;
import java.util.Arrays;
public class Debug03 { public static void main(String[] args) { int[] arr = {1, -1, 10, -20 , 100}; Arrays.sort(arr); for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + "\t"); } } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| package com.hspedu.debug_;
import java.util.Arrays;
public class Debug04 { public static void main(String[] args) {
int[] arr = {1, -1, 10, -20 , 100}; Arrays.sort(arr); for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + "\t"); }
System.out.println("hello100"); System.out.println("hello200"); System.out.println("hello300"); System.out.println("hello400"); System.out.println("hello500"); System.out.println("hello600"); System.out.println("hello700"); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| package com.hspedu.debug_;
public class DebugExercise { public static void main(String[] args) { Person jack = new Person("jack", 20); System.out.println(jack); } } class Person { private String name; private int age;
public Person(String name, int age) { this.name = name; this.age = age; }
@Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
|
十、练习

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
| package com.hspedu.homework;
public class Homework01 {
public static void main(String[] args) {
Person[] persons = new Person[3]; persons[0] = new Person("mary",30, "PHP工程师"); persons[1] = new Person("tom",50, "大数据工程师"); persons[2] = new Person("smith",10, "JavaEE工程师");
for (int i = 0; i < persons.length; i++) { System.out.println(persons[i]); }
Person tmp = null; for(int i = 0; i < persons.length -1 ;i++) { for(int j = 0; j < persons.length -1 - i; j++) { if(persons[j].getAge() > persons[j+1].getAge()) { tmp = persons[j]; persons[j] = persons[j+1]; persons[j+1]= tmp; }
} }
System.out.println("排序后的效果"); for (int i = 0; i < persons.length; i++) { System.out.println(persons[i]); }
}
}
class Person { private String name; private int age; private String job;
public Person(String name, int age, String job) { this.name = name; this.age = age; this.job = job; }
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 String getJob() { return job; }
public void setJob(String job) { this.job = job; }
@Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + ", job='" + job + '\'' + '}'; } }
|

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
| package com.hspedu.homework;
public class Teacher { private String name; private int age; private String post; private double salary; private double grade;
public Teacher(String name, int age, String post, double salary, double grade) { this.name = name; this.age = age; this.post = post; this.salary = salary; this.grade = grade; }
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 String getPost() { return post; }
public void setPost(String post) { this.post = post; }
public double getSalary() { return salary; }
public void setSalary(double salary) { this.salary = salary; }
public double getGrade() { return grade; }
public void setGrade(double grade) { this.grade = grade; } public void introduce() { System.out.println("name: " + name + " age: " + age + " post: " + post + " salary:" + salary + " grade:" + grade); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| package com.hspedu.homework;
public class Professor extends Teacher {
public Professor(String name, int age, String post, double salary, double grade) { super(name, age, post, salary, grade); }
@Override public void introduce() { System.out.println(" 这是教授的信息 "); super.introduce(); } }
|
1 2 3 4 5 6 7 8
| package com.hspedu.homework;
public class Homework03 { public static void main(String[] args) { Professor professor = new Professor("贾宝玉", 30, "高级职称", 30000, 1.3); professor.introduce(); } }
|

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| package com.hspedu.homework;
import java.sql.SQLOutput;
public class Employee {
private String name; private double daySal; private int workDays; private double grade;
public void printSal() { System.out.println(name + " 工资=" + daySal * workDays * grade); }
public Employee(String name, double daySal, int workDays, double grade) { this.name = name; this.daySal = daySal; this.workDays = workDays; this.grade = grade; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public double getDaySal() { return daySal; }
public void setDaySal(double daySal) { this.daySal = daySal; }
public int getWorkDays() { return workDays; }
public void setWorkDays(int workDays) { this.workDays = workDays; }
public double getGrade() { return grade; }
public void setGrade(double grade) { this.grade = grade; } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| package com.hspedu.homework;
public class Worker extends Employee{
public Worker(String name, double daySal, int workDays, double grade) { super(name, daySal, workDays, grade); }
@Override public void printSal() { System.out.print("普通员工 "); super.printSal(); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| package com.hspedu.homework;
public class Manager extends Employee { private double bonus; public Manager(String name, double daySal, int workDays, double grade) { super(name, daySal, workDays, grade); }
@Override public void printSal() { System.out.println("经理 " + getName() + " 工资是=" + (bonus + getDaySal() * getWorkDays() * getGrade())); }
public double getBonus() { return bonus; }
public void setBonus(double bonus) { this.bonus = bonus; } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| package com.hspedu.homework;
public class Homework04 { public static void main(String[] args) { Manager manage = new Manager("刘备", 100, 20, 1.2); manage.setBonus(3000); manage.printSal();
Worker worker = new Worker("关羽",50, 10, 1.0); worker.printSal();
} }
|

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| package com.hspedu.homework.homework5;
public class Employee { private String name; private double sal; private int salMonth = 12; public void printSal() { System.out.println(name + " 年工资是: " + (sal * salMonth)); }
public Employee(String name, double sal) { this.name = name; this.sal = sal; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public double getSal() { return sal; }
public void setSal(double sal) { this.sal = sal; }
public int getSalMonth() { return salMonth; }
public void setSalMonth(int salMonth) { this.salMonth = salMonth; } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| package com.hspedu.homework.homework5;
public class Peasant extends Employee {
public Peasant(String name, double sal) { super(name, sal); }
@Override public void printSal() { System.out.print("农民 "); super.printSal(); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| package com.hspedu.homework.homework5;
public class Scientist extends Employee{
private double bonus;
public Scientist(String name, double sal) { super(name, sal); }
@Override public void printSal() { System.out.print("科学家 "); System.out.println(getName() + " 年工资是: " + (getSal() * getSalMonth() + bonus)); }
public double getBonus() { return bonus; }
public void setBonus(double bonus) { this.bonus = bonus; } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| package com.hspedu.homework.homework5;
public class Teacher extends Employee{ private int classDays; private double classSal;
public Teacher(String name, double sal) { super(name, sal); }
@Override public void printSal() { System.out.print("老师 "); System.out.println(getName() + " 年工资是: " + (getSal() * getSalMonth() + classDays * classSal )); }
public int getClassDays() { return classDays; }
public void setClassDays(int classDays) { this.classDays = classDays; }
public double getClassSal() { return classSal; }
public void setClassSal(double classSal) { this.classSal = classSal; } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| package com.hspedu.homework.homework5;
public class Worker extends Employee{ public Worker(String name, double sal) { super(name, sal); }
@Override public void printSal() { System.out.print("工人 "); super.printSal(); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| package com.hspedu.homework.homework5;
public class Homework05 { public static void main(String[] args) { Worker jack = new Worker("jack", 10000); jack.setSalMonth(15); jack.printSal();
Peasant smith = new Peasant("smith", 20000); smith.printSal();
Teacher teacher = new Teacher("顺平", 2000); teacher.setClassDays(360); teacher.setClassSal(1000); teacher.printSal();
Scientist scientist = new Scientist("钟南山", 20000); scientist.setBonus(2000000); scientist.printSal(); } }
|

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| package com.hspedu.homework;
public class Homework07 { public static void main(String[] args) {
} } class Test{ String name = "Rose"; Test(){ System.out.println("Test"); } Test(String name){ this.name = name; } } class Demo extends Test{ String name="Jack"; Demo() { super(); System.out.println("Demo"); } Demo(String s){ super(s); } public void test(){ System.out.println(super.name); System.out.println(this.name); } public static void main(String[] args) { new Demo().test(); new Demo("john").test(); } }
|

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| package com.hspedu.homework;
public class BankAccount { private double balance ; public BankAccount(double initialBalance) { this.balance = initialBalance; } public void deposit(double amount) { balance += amount; } public void withdraw(double amount) { balance -= amount; }
public double getBalance() { return balance; }
public void setBalance(double balance) { this.balance = balance; } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| package com.hspedu.homework;
public class CheckingAccount extends BankAccount{ public CheckingAccount(double initialBalance) { super(initialBalance); }
@Override public void deposit(double amount) { super.deposit(amount - 1); }
@Override public void withdraw(double amount) { super.withdraw(amount + 1); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| package com.hspedu.homework;
public class SavingsAccount extends BankAccount {
private int count = 3; private double rate = 0.01;
public void earnMonthlyInterest() { count = 3; super.deposit(getBalance() * rate); }
@Override public void deposit(double amount) { if(count > 0) { super.deposit(amount); } else { super.deposit(amount - 1); } count--; }
@Override public void withdraw(double amount) { if(count > 0) { super.withdraw(amount); } else { super.withdraw(amount + 1); } count--; }
public SavingsAccount(double initialBalance) { super(initialBalance); }
public int getCount() { return count; }
public void setCount(int count) { this.count = count; }
public double getRate() { return rate; }
public void setRate(double rate) { this.rate = rate; } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| package com.hspedu.homework;
public class Homework08 { public static void main(String[] args) {
SavingsAccount savingsAccount = new SavingsAccount(1000); savingsAccount.deposit(100); savingsAccount.deposit(100); savingsAccount.deposit(100); System.out.println(savingsAccount.getBalance()); savingsAccount.deposit(100); System.out.println(savingsAccount.getBalance());
savingsAccount.earnMonthlyInterest(); System.out.println(savingsAccount.getBalance()); savingsAccount.withdraw(100); System.out.println(savingsAccount.getBalance()); savingsAccount.withdraw(100); savingsAccount.withdraw(100); System.out.println(savingsAccount.getBalance()); savingsAccount.deposit(100); System.out.println(savingsAccount.getBalance());
} }
|

1 2 3 4 5 6 7 8 9 10 11 12
| package com.hspedu.homework;
public class Point { private double x; private double y;
public Point(double x, double y) { this.x = x; this.y = y; } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13
| package com.hspedu.homework;
public class LabeledPoint extends Point { private String label;
public LabeledPoint(String label, double x, double y) { super(x, y); this.label = label; } }
|
1 2 3 4 5 6 7 8
| package com.hspedu.homework;
public class Homework09 { public static void main(String[] args) { new LabeledPoint("Black",1929,230.07); } }
|

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
| package com.hspedu.homework;
public class Doctor { private String name; private int age; private String job; private char gender; private double sal;
public Doctor(String name, int age, String job, char gender, double sal) { this.name = name; this.age = age; this.job = job; this.gender = gender; this.sal = sal; }
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 String getJob() { return job; }
public void setJob(String job) { this.job = job; }
public char getGender() { return gender; }
public void setGender(char gender) { this.gender = gender; }
public double getSal() { return sal; }
public void setSal(double sal) { this.sal = sal; }
public boolean equals(Object obj) { if (this == obj) { return true; } if (!(obj instanceof Doctor)) { return false; }
Doctor doctor = (Doctor)obj; return this.name.equals(doctor.name) && this.age == doctor.age && this.gender == doctor.gender && this.job.equals(doctor.job) && this.sal == doctor.sal;
} }
|
1 2 3 4 5 6 7 8 9 10 11 12
| package com.hspedu.homework;
public class Homework10 { public static void main(String[] args) { Doctor doctor1 = new Doctor("jack", 20, "牙科医生", '男', 20000); Doctor doctor2 = new Doctor("jack", 21, "牙科医生", '男', 20000);
System.out.println(doctor1.equals(doctor2)); } }
|


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| package com.hspedu.homework.homework13;
public class Person { private String name; private char gender; private int age;
public Person(String name, char gender, int age) { this.name = name; this.gender = gender; this.age = age; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public char getGender() { return gender; }
public void setGender(char gender) { this.gender = gender; }
public int getAge() { return age; }
public void setAge(int age) { this.age = age; }
public String play() { return name + "爱玩"; }
public String basicInfo() { return "姓名: " + name + "\n年龄: " + age + "\n性别: " + gender; }
@Override public String toString() { return "Person{" + "name='" + name + '\'' + ", gender=" + gender + ", age=" + age + '}'; } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| package com.hspedu.homework.homework13;
public class Student extends Person{
private String stu_id; public Student(String name, char gender, int age, String stu_id) { super(name, gender, age); this.stu_id = stu_id; }
public String getStu_id() { return stu_id; }
public void setStu_id(String stu_id) { this.stu_id = stu_id; }
public void study() { System.out.println(getName() + "承诺,我会好好学习 老韩讲的 java"); }
@Override public String play() { return super.play() + "足球"; }
public void printInfo() { System.out.println("学生的信息:"); System.out.println(super.basicInfo()); System.out.println("学号: " + stu_id); study(); System.out.println(play()); }
@Override public String toString() { return "Student{" + "stu_id='" + stu_id + '\'' + '}' + super.toString(); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| package com.hspedu.homework.homework13;
public class Teacher extends Person { private int work_age;
public Teacher(String name, char gender, int age, int work_age) { super(name, gender, age); this.work_age = work_age; }
public int getWork_age() { return work_age; }
public void setWork_age(int work_age) { this.work_age = work_age; }
public void teach() { System.out.println(getName() + "承诺,我会认真教学 java..."); }
@Override public String play() { return super.play() + "象棋"; } public void printInfo() { System.out.println("老师的信息:"); System.out.println(super.basicInfo()); System.out.println("工龄: " + work_age); teach(); System.out.println(play()); }
@Override public String toString() { return "Teacher{" + "work_age=" + work_age + '}' + super.toString(); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
| package com.hspedu.homework.homework13;
public class Homework13 { public static void main(String[] args) {
Teacher teacher = new Teacher("张飞", '男', 30, 5);
teacher.printInfo();
Student student = new Student("小明", '男', 15, "00023102"); System.out.println("-----------------------------------"); student.printInfo();
Person[] persons = new Person[4]; persons[0] = new Student("jack", '男', 10, "0001"); persons[1] = new Student("mary", '女', 20, "0002"); persons[2] = new Teacher("smith", '男', 36, 5); persons[3] = new Teacher("scott", '男', 26, 1);
Homework13 homework13 = new Homework13(); homework13.bubbleSort(persons);
System.out.println("---排序后的数组-----"); for(int i = 0; i < persons.length; i++) { System.out.println(persons[i]); }
System.out.println("======================="); for (int i = 0; i < persons.length; i++) { homework13.test(persons[i]); }
}
public void test(Person p) { if(p instanceof Student) { ((Student) p).study(); } else if(p instanceof Teacher) { ((Teacher) p).teach(); } else { System.out.println("do nothing..."); } }
public void bubbleSort(Person[] persons) { Person temp = null; for (int i = 0; i < persons.length - 1; i++) { for (int j = 0; j < persons.length - 1 - i; j++) { if(persons[j].getAge() < persons[j+1].getAge()) { temp = persons[j]; persons[j] = persons[j + 1]; persons[j + 1] = temp; } } } }
}
|

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| package com.hspedu.homework;
public class Homework15 { public static void main(String[] args) { AAA obj = new BBB(); AAA b1 = obj; System.out.println("obj的运行类型=" + obj.getClass()); obj = new CCC();
System.out.println("obj的运行类型=" + obj.getClass()); obj = b1;
System.out.println("obj的运行类型=" + obj.getClass()); } }
class AAA {
} class BBB extends AAA {
} class CCC extends BBB {
}
|