子类构造方法调用父类构造方法(super关键字的使用)

本文通过一个Java程序示例展示了不同教育阶段学生的特点及行为差异。主要介绍了基础的学生类,并通过本科生和研究生子类来具体实现各自特有的属性和方法。每个类都包含了展示自身信息的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package kaoshi;

/**
 ************************************
 * @author Hejing
 * @date   2017年12月24日
 * @class  fisrt.java
 * 
 ************************************
 */
class Student{
	String name;
	int age;
	
	public Student(String name,int age) {
		this.name=name;
		this.age=age;
		
	}
	
	public void study() {
		System.out.println("学生的学习方法是:");
	}
	public void show() {
		System.out.println("姓名是"+name);
		System.out.println("年龄是"+age);	
	}
}
class Benkesheng extends Student{
	String dept;
	String disagree;
	public Benkesheng(String name,int age,String dept,String disagree) {
		super(name, age);
		this.dept=dept;
		this.disagree=disagree;
		
	}
	
	public void study() {
		System.out.println("本科生学习方法是:");
	}
	public void show() {
		super.show();
		System.out.println("专业是"+dept);
		System.out.println("学位是"+disagree);
	}
}
class Yanjiusheng extends Benkesheng{
	String direction;
	public  Yanjiusheng(String name,int age,String dept,String disagree,String direction) {
		super(name, age,dept,disagree);
		this.direction =direction;
		
	}
	
	public void study() {
		System.out.println("研究生学习方法是:");
	}
	public void show() {
		super.show();
		System.out.println("方向"+direction);
	}
}
public class fisrt {
public static void main(String[] args) {
	Student s1=new Student("张三",18);
	Benkesheng b1=new Benkesheng("张思",20,"计算机","软件学位");
	Yanjiusheng y1=new  Yanjiusheng("张wu",20,"计算机","软件学位","人工智能");
	s1.study();
	s1.show();
	b1.study();
	b1.show();
	y1.study();
	y1.show();
	
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值