面向对象继承之间的传递性

1、先写爷爷类

package com.wyq.study;

public class B {
	private String  name;
	private int 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 B(String name, int age) {
		super();
		this.name = name;
		this.age = age;
		System.out.println("B的实参构造");
	}
	public B() {
		super();
		System.out.println("B的无参构造");
	}
}

2、再写父亲类

package com.wyq.study;

public class C extends B{
	private String job;

	public String getJob() {
		return job;
	}

	public void setJob(String job) {
		this.job = job;
	}

	public C(String name, int age, String job) {
		super(name, age);
		this.job = job;
		System.out.println("这里是C的带参构造。");
	}

	public C() {
		super();
		System.out.println("这里是C的无参构造。");
	}
	public void say(String place){
		System.out.println("C正在"+place+"地方说话。"+"他的工作是"+job);
	}

}

3、接着写孙子类

package com.wyq.study;

public class D extends C{
	private String course;
	private int teachAge;
	public String getCourse() {
		return course;
	}
	public void setCourse(String course) {
		this.course = course;
	}
	public int getTeachAge() {
		return teachAge;
	}
	public void setTeachAge(int teachAge) {
		this.teachAge = teachAge;
	}
	
	
	
	public D(String name, int age, String job,String course, int teachAge) {
		super(name, age, job);
		this.course = course;
		this.teachAge = teachAge;
		System.out.println("这里是"+name+"\t"+age+"\t"+job+"\t"+course+"\t"+teachAge);
	}
	
	public D() {
		super();
		System.out.println("D的无参构造");
	}
	public void speak(String home,int age){
		System.out.println("参数为:"+course+"\t"+age+"\t"+this.getCourse()+"\t"+this.getCourse()+"\t"+this.getName()+"\t"+this.getAge());
		System.out.println("以下是超类测试");
		System.out.println("这里是超类的测试"+super.getAge()+"\t"+super.getName()+"\t"+super.getJob()+"\t");
	
	}
	

}

4、最后测试

package com.wyq.study;

public class testD {
	public static void main(String[] args) {
		D d = new D("张三",10,"程序员","计算机",15);
		d.speak("北京市", 10);
		
	}

}

5、总结

1)先写父类

2)再写子类,子类继承父类,创建无参和有参构造,初始化属性

3)再写孙子类,也是继承的关系,创建无参和有参构造,初始化属性,调用父类属性使用super关键字

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值