Java中的super关键字
示例一
要点
1、super是一个关键字,全部小写。
2、super和this对比着学习。
this:
this能出现在实例方法和构造方法中
this的语法是:"this."、"this()"
this不能使用在静态方法中
this.大部分情况下是可以省略的
this.什么时候不能省略呢?在区分局部变量和实例变量的时候不能省略。
publie void setName(String name){
this.name=name;
}
this()只能出现在构造方法第一行,通过当前的构造方法去调用"本类"中其它的构造方法,目的是:代码复用。
super:
super能出现在实例方法和构造方法中。
super的语法是:"super.","super()"
super不能使用在静态方法中。
super.大部分情况下是可以省略的。
super.什么时候不能省略呢?在区分局部变量和实例变量的时候不能省略。
super()只能出现在构造方法第一行,通过当前的构造方法去调用父类”中的构造方法,目的是:创建子类对象的时候,先初始化父类型特征。
实例
public class SuperTest01{
public static void main(String[] args){
//创建子类对象
new B();
}
}
class A extends Object{//所有类默认继承Object类
//建议手动的将一个类的无参数构造方法写出来。
public A(){
//super(); //这里也是默认有这一行代码的。
System.out.println ("A类的无参数构造方法!");
}
//一个类如果没有手动提供任何构造方法,系统会默认提供一个无参数构造方法。
//一个类如果手动提供了一个构造方法,那么无参数构造系统将不再提供。
public A(int i){
//super();
System.out.println("A类的有参数构造方法(int)");
}
}
class B extends A{
/*
public B(){
super();
System.out.println("B类的无参数构造方法!");
}
*/
public B(){
this("zhangsan");
//调用父类中有参数的构造方法
//super(123);
System.out.println("B类的无参数构造方法!");
}
public B(String name){
super();
System.out.println("B类的有参数构造方法(string) ");
}
}
示例二
要点
1、举个例子:在恰当的时间使用 super(实际参数列表);
2、注意:在构造方法执行过程中一连串调用了父类的构造方法,父类的构造方法又继续向下调用它的父类的构造方法,但是实际上对象只创建了一个。
3、思考:"super(实参)"到底是干哈的?
super(实参)的作用是:初始化当前对象的父类型特征。并不是创建新对象。实际上对象只创建了1个
4、super关键字代表的就是当前对象中的父类型特征。
实例
public class SuperTest02{
public static void main(String[] args){
CreditAccount ca1 = new CreditAccount();
System.out.println(ca1.getActno() + ","+ ca1.getBalance()+ "," + ca1.getCredit())
CreditAccount ca2 = new CreditAccount("1111",10000.0,0.999);
System.out.println (ca2.getActno() + "," + ca2.getBalance () + "," +ca2.getCredit())
}
}
//账户
class Account{
//属性
private String actno;
private double balance;
//构造方法
public Account(){
/*
//默认初始化:
super();
this.actno =null;
this.balance = 0.0;
*/
}
public Account (String actno,double balance){
this.actno=actno;
this.balance=balance;
}
}
//信用账户
class CreditAccount extends Account{
//属性:信誉度(诚信值)
//子类特有的一个特征,父类没有
private double credit;
//构造方法
//分析以下程序是否存在编译错误????
public CreditAccount(String actno,double balance,double credit){
//私有的属性,只能在本类中访问
/*
this.actno = actno;
this.balance = balance;
*/
//以上两行代码在恰当的位置,正好可以使用:super(actno,balance);
//通过子类的构造方法调用父类的构造方法。
super(actno, balance);
this.credit=credit;
}
//提供无参数的构造方法
public CreditAccounto{}
}
内存图
示例三
要点
1、"this."和"super."大部分情况下都是可以省略的
2、this.什么时候不能省略?
public void setName (string nane){
this.name=name;
}
3、super.什么时候不能省略?
父中有,子中又有,如果想在子中访问"父的特征",super.不能省略。
实例
public class SuperTest03{
public static void main(String[] args){
Vip v=new vip("张三");
v.shopping();
}
}
class Customer{
String name;
public Customer(){}
public Customer(String name){
super();
this.name=name;
}
public void dosome(){
System.out.println (this.name +" do some! ");
System.out.println (name + " do some! ");
//错误:找不到符号
//System.out.println (super.name + " do some ! ");
}
}
class Vip extends Customer{
//假设子类也有一个同名属性
//java中允许在子类中出现和父类一样的同名变量/同名属性。
String name; //实例变量
public vip(){}
public Vip(String name){
super(name);
//this.name = null;
}
public void shopping(){
/*
java是怎么来区分子类和父类的同名属性的?
this.name:当前对象的name属性
super.name:当前对象的父类型特征中的name属性。
*/
System.out.println(this.name +"正在购物!");// null正在购物
System.out.println(super.nane + "正在购物!");//张三正在购物
System.out.println (name +"正在购物!");//nul正在购物
}
}
内存图
示例四
要点
super不是引用。super也不保存内存地址,super也不指向任何对象。
super只是代表当前对象内部的那一块父类型的特征
在父和子中有同名的属性,或者说有相同的方法,如果此时想在子类中访问父中的数据,必须使用"super."加以区分,子类中不能通过super.访问父类中的私有属性方法
super.属性名【访问父类的属性】
super.方法名(实参)【访问父类的方法】
super(实参)【调用父类的构造方法】
实例
public class SuperTest04{
//实例方法
public void dosome(){
System.out.println(this); //SuperTest0602f92e0f4
//输出"引用"的时候,会自动调用引用的toString()方法:System.out.println(this.toString());
//编译错误:需要"." super不是引用
//System.out.println(super);
}
// this和super不能使用在static静态方法中
/*
public static void doother(){
System.out.println(this);
System.out.println(super.xxx);
}
*/
//静态方法,主方法
public static void main(String[] args){
SuperTest04 st = new SuperTest04();
st.dosome();
}