定义一个员工类,一个继承员工类的经理类。在主方法中创建一个经理对象,并调用经理类当中的成员方法。
class Employee{
public void work() { // 成员方法
System.out.println("员工在工作");
}
}
class Manager extends Employee{
//子类重写父类中的work方法
public void work() {
System.out.println("经理在工作");
}
public void method1() { // 成员方法
//this.work();
work();
super.work();
}
}
public class Test01{
public static void main(String[] args) {
Manager m=new Manager();
m.method1();
}
}
运行结果为:
经理在工作
员工在工作
现在在员工类里创建一个字符串类型名字:
class Employee{
String name="张三";
public void work() { // 成员方法
System.out.println("员工在工作");
}
}
class Manager extends Employee{
//String name="李四";
//子类重写父类中的work方法
public void work() {
System.out.println("经理在工作");
}
public void method1() { // 成员方法
//this.work();
work();
super.work();
System.out.println(this.name);
System.out.println(name);
System.out.println(super.name);
}
}
public class Test01{
public static void main(String[] args) {
Manager m=new Manager();
m.method1();
}
}
运行结果为:
经理在工作
员工在工作
张三
张三
张三
再在经理类中创建一个字符串类型名字:
class Employee{
String name="张三";
public void work() { // 成员方法
System.out.println("员工在工作");
}
}
class Manager extends Employee{
String name="李四";
//子类重写父类中的work方法
public void work() {
System.out.println("经理在工作");
}
public void method1() { // 成员方法
//this.work();
work();
super.work();
System.out.println(this.name);
System.out.println(name);
System.out.println(super.name);
}
}
public class Test01{
public static void main(String[] args) {
Manager m=new Manager();
m.method1();
}
}
运行结果为:
经理在工作
员工在工作
李四
李四
张三
1.super不是引用类型,super中存储的不是内存地址,this是。super指向的不是父类对象。
2.super代表的是当前子类对象中父类型特征,如果子类没有显示地又定义了一个这样的特征(属性方法),用不用super也无所谓了(如博客中实例2)。
3.什么时候使用super?
子类和父类中都有某个数据,例如,子类和父类中都有name这个属性
如果要在子类中访问父类中的name属性,需要使用super.
4.super可以用在什么地方?
第一:可以用在成员方法
第二:可以用在构造方法
5.this和super相同,都不能用在静态方法中
下面我们看一个代码实例,注释中会给出注意事项:
/*
super关键字用在构造方法中:
语法:super(实参);
作用:通过子类的构造方法去调用父类的构造方法。
语法规则:一个构造方法第一行如果没有this(...),也没有显式调用super(...)
系统会默认调用super()
注意:super()的调用只能放在构造方法的第一行
super()和this()不能共存
super()调用了父类中的构造方法,但是并不会创建父类对象
在Java语言中只要是创建Java对象,那么Object中的无参构造方法一定会执行
*/
class Account{
//Field
private String actno;
private double balance;
//Constructor
public Account() {
//super();这里其实也有,因为Account继承了Object类
System.out.println("Account的无参构造方法执行");
}
public Account(String acton,double balance) {
this.actno=acton;
this.balance=balance;
}
public String getActno() {
return actno;
}
public void setActno(String actno) {
this.actno = actno;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
}
class DebitAccount extends Account{
//Field
//独特属性
private double debit;
//Constructor
public DebitAccount() {
//super();
}
public DebitAccount(String acton,double balance,double debit) {
//通过子类的构造方法去调用父类的构造方法,作用是:给当前子类对象中的父类型特征赋值
super(acton,balance);
//构造方法执行不一定创建对象
this.debit=debit;
}
//setter and getter
public double getDebit() {
return debit;
}
public void setDebit(double debit) {
this.debit = debit;
}
}
public class Test01{
public static void main(String[] args) {
DebitAccount da=new DebitAccount();
}
}
运行结果是:
Account的无参构造方法执行