接口创建和实现

创建一个Study接口

public interface Study {
	void studyLine();//抽象方法studyLine
	
}

创建一个普通类Line

public class Line {
	private String emplymentClass;//班级
	private int money;//学费

	public Line() {//无参构造
	}
	public Line(String emplymentClass, int money) {//有参构造
		this.emplymentClass = emplymentClass;
		this.money = money;
	}
	public String getEmplymentClass() {
		return emplymentClass;
	}
	public void setEmplymentClass(String emplymentClass) {
		this.emplymentClass = emplymentClass;
	}
	public int getMoney() {
		return money;
	}
	public void setMoney(int money) {
		this.money = money;
	}
}

创建Student实现类实现Study

public class Student implements Study{
	private String name;//姓名
	private double score;//成绩
	private  Line lin;//Line作为参数

	public Student(String name, double score, Line lin) {//有参构造
		this.name = name;
		this.score = score;
		this.lin = lin;
	}
	public Student() {}//无参构造	
	public String getName() {//gteter/setter方法
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public double getScore() {
		return score;
	}
	public void setScore(double score) {
		this.score = score;
	}
    public Line getLin() {
		return lin;
	}
	public void setLin(Line lin) {
		this.lin = lin;
	}

	@Override
	public void studyLine() {//覆写study接口的studyLine方法
    System.out.println("您报名的就业班为:"+lin.getEmplymentClass()+",应缴纳学费:"+lin.getMoney()+"元。");
	}
	
	void exam() {//判断成绩是否及格方法
		if(score>=60) {
			System.out.println("您的成绩为"+score+",考试通过");
		}else {
			System.out.println("您的成绩为"+score+",考试不通过");
		}
	}
}

创建一个测试类

public class Test {
	public static void main(String[] args) {
		Line lintest = new Line("三班",1998);//new一个Line的对象,并通过有参构造赋值
		Student stu = new Student();//new一个Student对象
		stu.setName("張三");//赋值名字
		stu.setScore(88);//赋值成绩
		stu.setLin(lintest);//班级和学费
		
		stu.exam();//调用判断成绩是否合格的方法
		stu.studyLine();//调用studyLine方法
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值