運動員與教練例項

進階大佬之路發表於2020-10-11


前言

運動員與教練案例


提示:以下是本篇文章正文內容,下面案例可供參考

一、定義說英語介面

程式碼如下:

package jiekou;

public interface speak {
public abstract void english();
}

二、定義抽象人類

程式碼如下:

package jiekou;

public abstract class person {
	private int age;
	private String name;
	public person() {
		super();
		// TODO Auto-generated constructor stub
	}
	public person(int age, String name) {
		super();
		this.age = age;
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public abstract void eat();
}

三.定義抽象教練,繼承人類

程式碼如下(示例):

package jiekou;

public abstract class jiaolian extends person {

	public jiaolian() {
		super();
		// TODO Auto-generated constructor stub
	}

	public jiaolian(int age, String name) {
		super(age, name);
		// TODO Auto-generated constructor stub
	}
	public abstract void teach();
}

四、定義抽象運動員,繼承人類

package jiekou;

public abstract class player extends person {

	public player() {
		super();
		// TODO Auto-generated constructor stub
	}

	public player(int age, String name) {
		super(age, name);
		// TODO Auto-generated constructor stub
	}

	public abstract void study();

}

五、定義具體籃球教練,繼承教練類

package jiekou;

public class jiaol extends jiaolian {

	public jiaol() {
		super();
		// TODO Auto-generated constructor stub
	}

	public jiaol(int age, String name) {
		super(age, name);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void teach() {
		// TODO Auto-generated method stub
		
		System.out.println("籃球教練教籃球");
	}

	@Override
	public void eat() {
		// TODO Auto-generated method stub
		System.out.println("吃的鮑魚");
	}

}

六、定義具體乒乓球教練類,繼承教練類,實現說英語介面

package jiekou;

public class jiaobingp extends jiaolian implements speak{

	
	public jiaobingp() {
		super();
		// TODO Auto-generated constructor stub
	}

	public jiaobingp(int age, String name) {
		super(age, name);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void teach() {
		// TODO Auto-generated method stub
		System.out.println("兵乓球教練教乒乓");
	}

	@Override
	public void eat() {
		// TODO Auto-generated method stub
System.out.println("火鍋");
	}
	public void english() {
		System.out.println("乒乓球教練說英語");
	}
	
}

七、定義具體籃球運動員類,繼承運動員類

package jiekou;

public class basketball extends player {

	public basketball() {
		super();
		// TODO Auto-generated constructor stub
	}

	public basketball(int age, String name) {
		super(age, name);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void study() {
		// TODO Auto-generated method stub
System.out.println("籃球運動員要學籃球");
	}

	@Override
	public void eat() {
		// TODO Auto-generated method stub
System.out.println("籃球運動員吃白米粥");
	}

}

八、定義具體乒乓球遠動員類,繼承運動員類,實現說英語介面

package jiekou;

public class pingpang extends player implements speak{

	public pingpang() {
		super();
		// TODO Auto-generated constructor stub
	}

	public pingpang(int age, String name) {
		super(age, name);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void study() {
		// TODO Auto-generated method stub
		System.out.println("足球運動員學習足球和英語");
	}

	@Override
	public void eat() {
		// TODO Auto-generated method stub
		System.out.println("乒乓球運動員吃 燒烤");
	}
	public void english() {
		System.out.println("乒乓運動員要學習英語");
	}

}

九、定義測試類,寫程式碼測試

package jiekou;

public class demo {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		jiaol bask=new jiaol();
		bask.setAge(35);
		bask.setName("籃球教練");
		System.out.println(bask.getAge()+","+bask.getName());
		bask.eat();bask.teach();
		
		System.out.println("-----------");
		jiaobingp pp=new jiaobingp();
		pp.setAge(45);
		pp.setName("乒乓教練");
		System.out.println(pp.getAge()+","+pp.getName());
		pp.eat();pp.teach();pp.english();
		
		System.out.println("-----------");
		basketball bap=new basketball();
		bap.setAge(17);
		bap.setName("籃球運動員");
		System.out.println(bap.getAge()+","+bap.getName());
		bap.eat();bap.study();
		
		System.out.println("-----------");
		pingpang ppp=new pingpang();
		ppp.setAge(18);
		ppp.setName("乒乓運動員");
		System.out.println(ppp.getAge()+","+ppp.getName());
		ppp.eat();ppp.study();ppp.english();
		
		System.out.println("-----------");
		ppp=new pingpang(19,"張三");
		System.out.println(ppp.getAge()+","+ppp.getName());
		ppp.eat();ppp.english();ppp.study();
	}

}

總結

提示:本例題是看了b站上的 黑馬程式設計師 java基礎中的例題,自己手動敲了一遍後再看他的案例實現。
主要學會介面的運用,抽象類中的方法要想實現的話必須依靠他的子類來實現 。

相關文章