/*
- 方法的重写
- 1,定义:在子类中可以根据需要对从父类中继承来的方法进行改造,也称为方法的重置,覆盖。
- 在程序执行时,子类的方法会覆盖父类的方法。
- 2,重写的规则
- 2.1 子类重写的方法必须和父类被重写方法具有相同的方法名称,参数列表
- 方法的重载是同类下,同名方法参数列表不同
- 2.2子类重写的方法返回值类型不能大于父类被重写方法的返回值类型
- 1,父类返回值类型为void,子类重写的返回值类型也是void
- 2,父类被重写方法的返回值类型是A(引用数据类型),则子类重写方法的返回值类型可以是A类或A的子类。
- 3,父类被重写方法的返回值类型是基本数据类型。则子类重写方法的返回值类型是相同的基本数据类型。
- 2.3子类重写方法的访问权限不能小于父类被重写方法的访问权限
- 子类不能重写父类中private权限的方法,子类可以创建方法名与参数列表与父类中private方法相同的方法。
- 但这不会被认为是重写
- 方法中调用的方法如果是重写的,则执行子类中重写的方法。
- 2.4子类方法抛出的异常类型不能大于父类被重写方法的异常类型。
- 3,说明
- 子类与父类中同名同参的方法必须同时声明为非static的(即重写的情况)。或者同时声明为
- static的(不是重写),因为static 的方法属于类的,子类无法覆盖父类的static方法。
- 实际开发常常直接复制父类方法,然后重写方法体,权限与返回值不用改变。
- 在eclips中可以用alt+/选择自动生成方法重写结构,然后更改方法体即可。
- super关键字的使用:
- super可以用super.属性或者super.方法的方式来显示的调用父类的属性方法和构造器,但通常会省略super。
- 1.当子类和父类中定义了同名的属性时,想要在子类中调用父类的属性,必须显式的使用super关键字表明调用的是父类的属性。
- 2.当子类中重写了父类的方法后,想要在子类中调用父类被重写的方法时,必须显式的使用super关键字表明调用的是父类的被重写的方法。
- 对于没有重写的方法,使用this与super声明效果相同。因为this调用的成员在子类中找不到时会到父类中去找。
- 注意super的追溯不限于直接父类
- 3,super调用构造器:
- 3.1可以在子类的构造器中显式的使用super(形参列表)的方式,调用父类中指定的构造器。
- 3.2super调用构造器与this一样只能放在首行。不能同时调用this(形参列表)和super(形参列表)声明的构造器。
- 3.3子类的构造器默认调用父类的空参构造器super()(即使删除也会调用),也会执行父类空参构造器中设定的程序。
- 3.4推理因为类的n个构造器中至多有n-1个构造器首行使用this(形参列表)方式调用其他构造器,
- 根据3.2和3.3,类的构造器中至少有一个构造器使用super(形参列表)的方式调用父类的构造器。
- 3.5如果父类中没有空参构造器,子类构造器无法调用,会报错。
- 子类的对象实例化过程分析;
- 1,从结果看,子类继承父类后就获取了父类中声明的属性和方法,创建子类的对象就会在堆中加载所有直接或间接父类中声明的属性。
- 2,从过程看,当我们通过子类的构造器创建对象时,一定会直接或间接的调用其父类的构造器,进而调用父类的父类的构造器直到
- 调用java.lang.Object类的构造器,正因为加载过这些构造器,所以内存空间中才会有直接或间接父类的属性和方法。子类对象
- 才可以去调用。
- 3,创建子类对象时虽然调用了父类的构造器,但过程中只创建了一个对象,即子类new 的对象。
*/
package object_chapter2;
public class Object_Override {
public static void main(String[] args) {
Student s = new Student();
new SubStudent();
s.setAge(35);
System.out.println(s.getNewAge());
System.out.println(s.getNewAge1());
s.readInfo();
//测试向下类型转换
People2 p = new SubStudent();
p.eat();
System.out.println(p instanceof SubStudent);//显示true
SubStudent ss = (SubStudent) p;//转换成功,可以调用孙类的方法
ss.cry();
Student ss1 = new Student();
System.out.println(ss1 instanceof SubStudent);//显示false
SubStudent ss2 = (SubStudent)ss1;//会报错
}
}
class People2{
public String name = "defaultname";
private int age = 28;
int number = 1000;
protected int argus = 11;
public String ID = "身份证号13311113331313";
public People2() {
System.out.println("初代构造器");
}
public People2(String name, int age, int number, int argus,String ID) {
this.name = name;
this.age = age;
this.number = number;
this.argus = argus;
this.ID = ID;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public int getArgus() {
return argus;
}
public void setArgus(int argus) {
this.argus = argus;
}
private void show() {
System.out.println("秀儿");
}
public void say() {
System.out.println("Hello.");
}
public int getNewAge() {
say();//如果子类重写此方法则子类的对象调用此方法(getNewAge())时使用子类重写的方法
show();//私有方法不能被重写,子类对象调用此方法(getNewAge())时使用父类方法
return age - 2;
}
public Object getInfo(){
return null;
}
public double Info(){
return 2.2;
}
public void setInfo() {
System.out.println("墙都不服");
}
}
class Student extends People2{
public String ID = "学号:23";
public Student() {
System.out.println("一代构造器");
}
public Student(String name, int age, int number, int argus,String ID,String ID1) {
super(name, age,number,argus,ID);
this.ID = ID1;
}
public void show() {
System.out.println("秀你一脸");
}
public void say() {
System.out.println("Hi!");
}
public int getNewAge1() {
super.say();
show();
return getAge() - 10;
}
public String getInfo(){//String类是Object 的子类,可以重写
return null;
}
public double Info(){//返回值为基本数据类型时,重写的方法返回值与被重写方法返回值类型一致。
return 3.3;
}
public void readInfo() {
System.out.println(super.ID + " " + ID);
super.setInfo();
System.out.println("舅服你");
}
}
class SubStudent extends Student{
public SubStudent() {
System.out.println("我是构二代");
}
}
/*
- java继承性的练习
- 1,通过Circle与Cylinder类练习继承性与方法的重写
- 2,创建Account与CheckAccount类综合练习
*/
package object_chapter2;
public class Extends_Exercise {
public static void main(String[] args) {
Circle c = new Circle(2);
System.out.println("半径为" + c.getRadius() + "的圆的面积是:" + c.getArea());
Cylinder c1 = new Cylinder(10);
c1.setRadius(2);
System.out.println("半径为" + c1.getRadius() + "的圆柱的表面积是:" + c1.getArea());
System.out.println("半径为" + c1.getRadius() + "高度为" + c1.length + "的圆柱的体积是:" + c1.getVolume());
Account a = new Account(1,20000,4.5);
a.withdraw(1000);
System.out.println("当前账户余额为" + a.getBalance());
a.withdraw(21000);
System.out.println("当前账户余额为" + a.getBalance());
a.withdraw(5000);
System.out.println("当前账户余额为" + a.getBalance());
CheckAccount ck = new CheckAccount(2,20000,4.4,10000);
ck.withdraw(10000);
System.out.println("当前账户余额为" + ck.getBalance() + "可透支金额为" + ck.getOverdraft());
ck.withdraw(30000);
System.out.println("当前账户余额为" + ck.getBalance() + "可透支金额为" + ck.getOverdraft());
ck.withdraw(15000);
System.out.println("当前账户余额为" + ck.getBalance() + "可透支金额为" + ck.getOverdraft());
}
}
class Circle {
double radius;
public Circle(){
super();
}
public Circle(double radius) {
super();
this.radius = radius;
}
public double getArea() {
return Math.PI * radius * radius;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
}
class Cylinder extends Circle{
double length;
public Cylinder() {
super();
}
public Cylinder(double length) {
super();
this.length = length;
}
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
public double getArea() {
return 2 * super.getArea() + length * Math.PI * 2 * getRadius();
}
public double getVolume() {
return super.getArea() * length;
}
}
class Account{
private int id;
private double balance;
private double annualInterestRate;
public Account() {
super();
}
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 void withdraw (double amount) {
if(balance >= amount) {
balance -= amount;
}else {
System.out.println("余额不足");
}
}
public void deposit (double amount) {
if(amount > 0) {
balance += amount;
}
}
}
class CheckAccount extends Account{
private double overdraft;
public CheckAccount() {
super();
}
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;
}
public void withdraw (double amount) {
if(getBalance() >= amount) {
super.withdraw(amount);
}else if(overdraft + getBalance() >= amount){
setOverdraft(overdraft + getBalance() - amount);
super.withdraw(getBalance());
}else {
System.out.println("透支超限");
}
}
}