super的那些奇怪的事

1、super是一个关键字,全部小写。
    2、super和this对比着学习。
        this:
            this能出现在实例方法和构造方法中。
            this的语法是:“this.”、“this()”
            this不能使用在静态方法中。
            this. 大部分情况下是可以省略的。
            this.什么时候不能省略呢? 在区分局部变量和实例变量的时候不能省略。
                public void setName(String name){
                    this.name = name;
                }
            this() 只能出现在构造方法第一行,通过当前的构造方法去调用“本类”中
            其它的构造方法,目的是:代码复用。

        super:
            super能出现在实例方法和构造方法中。
            super的语法是:“super.”、“super()”
            super不能使用在静态方法中。
            super. 大部分情况下是可以省略的。
            super.什么时候不能省略呢? ???????
            super() 只能出现在构造方法第一行,通过当前的构造方法去调用“父类”中
            的构造方法,目的是:创建子类对象的时候,先初始化父类型特征。

    3、super()
        表示通过子类的构造方法调用父类的构造方法。
        模拟现实世界中的这种场景:要想有儿子,需要先有父亲。
    
    4、重要的结论:
        当一个构造方法第一行:
            既没有this()又没有super()的话,默认会有一个super();
            表示通过当前子类的构造方法调用父类的无参数构造方法。
            所以必须保证父类的无参数构造方法是存在的。
    
    5、注意:
        this()和super() 不能共存,它们都是只能出现在构造方法第一行。
    
    6、无论是怎样折腾,父类的构造方法是一定会执行的。(百分百的。)

 

 

 

 super和this不能出现在main方法等静态方法中

 new B() 看看结果

public class SuperTest01 {
    public static void main(String[] args) {
        new B();
    }
}
class A{
    int id;
    public A() {
        System.out.println("父类的无参构造");
    }

    public A(int id) {
        System.out.println("父类的有参构造");
    }
}
class B extends A{
    public B() {
        System.out.println("子类的无参构造");
    }

    public B(int id) {
        System.out.println("子类的无参构造");
    }
}

一个类如果没有手动提供任何构造方法,系统会默认提供一个无参数构造方法。 

 改动一下

 大家要注意:
        以后写代码的时候,一个类的无参数构造方法还是建议大家手动的写出来。
        如果无参数构造方法丢失的话,可能会影响到“子类对象的构建”。

判断程序的输出顺序

public class SuperTest01 {
    public static void main(String[] args) {
        new C();
    }
}
class A extends Object{
    public A(){
        System.out.println("1"); 
    }
}

class B extends A{
    public B(){
        System.out.println("2"); 
    }
    public B(String name){
        super();
        System.out.println("3"); 
    }
}

class C extends B{
    public C(){ // 这个是最先调用的。但是最后结束。
        this("zhangsan");
        System.out.println("4");
    }
    public C(String name){
        this(name, 20);
        System.out.println("5");
    }
    public C(String name, int age){
        super(name);
        System.out.println("6");
    }
}

 顺序是1--3---6---5----4

在父和子中有同名的属性,或者说有相同的方法,
    如果此时想在子类中访问父中的数据,必须使用“super.”加以区分。

    super.属性名    【访问父类的属性】
    super.方法名(实参) 【访问父类的方法】
    super(实参)  【调用父类的构造方法】

什么时候用super

/*
	1、举个例子:在恰当的时间使用:super(实际参数列表);
	2、注意:在构造方法执行过程中一连串调用了父类的构造方法,
	父类的构造方法又继续向下调用它的父类的构造方法,但是实际上
	对象只创建了一个。

	3、思考:“super(实参)”到底是干啥的?
		super(实参)的作用是:初始化当前对象的父类型特征。
		并不是创建新对象。实际上对象只创建了1个。
	
	4、super关键字代表什么呀?
		super关键字代表的就是“当前对象”的那部分父类型特征。

		我继承了我父亲的一部分特征:
			例如:眼睛、皮肤等.
			super代表的就是“眼睛、皮肤等”。
			“眼睛、皮肤等”虽然是继承了父亲的,但这部分是在我身上呢。

*/
// 测试程序
public class SuperTest03{
	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 extends Object{
	// 属性
	private String actno;
	private double balance;

	// 构造方法
	public Account(){
		//super();
		//this.actno = null;
		//this.balance = 0.0;
	}
	public Account(String actno, double balance){
		// super();
		this.actno = actno;
		this.balance = balance;
	}

	// setter and getter
	public void setActno(String actno){
		this.actno = actno;
	}
	public String getActno(){
		return actno;
	}
	public void setBalance(double balance){
		this.balance = balance;
	}
	public double getBalance(){
		return 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 CreditAccount(){
		//super();
		//this.credit = 0.0;
	}

	// setter and getter方法
	public void setCredit(double credit){
		this.credit = credit;
	}
	public double getCredit(){
		return credit;
	}
	
}

 想想输出为什么一样的,子类继承了父类的name,this.  与  super  都表示一样的对象

public class SuperTest04{
	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;
	}
}
class Vip extends Customer{
	public Vip(){}
	public Vip(String name){
		super(name);
	}
	// super和this都不能出现在静态方法中。
	public void shopping(){
		// this表示当前对象。
		System.out.println(this.name + "正在购物!");
		// super表示的是当前对象的父类型特征。(super是this指向的那个对象中的一块空间。)
		System.out.println(super.name + "正在购物!");
		System.out.println(name + "正在购物!");
	}
}

如果子类重写了父类的方法,在调用父类方法必须要用super,this.name或者name访问的就是子类

 

/*
	1、“this.”和“super.”大部分情况下都是可以省略的。
	2、this. 什么时候不能省略?
		public void setName(String name){
			this.name = name;
		}
	3、super. 什么时候不能省略?
		父中有,子中又有,如果想在子中访问“父的特征”,super. 不能省略。
*/
public class SuperTest05{
	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.name + "正在购物!"); // 张三正在购物
		System.out.println(name + "正在购物!"); //null 正在购物
	}
}

 为什么当前对象时空呢,因为关键的一步没有了,this.name = name;而是super去了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值