一个关于常用类的简单小练习
分析
1.首先教练属于人先定义一个人的类
2.写一些人都有的功能
private String name;
private int age;
//构造方法
public Person(){
}
public Person(String name , int age){
this.name = name ;
this.age = age;
}
//getXX和setXX
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 abstract void eat ();
public abstract void sleep ();
写完人类之后写教练类
教练是人,所以有人有的功能
可以继承人的功能
public abstract class Muster extends Person{
public Muster(){
}
public Muster(String name , int age){
super (name , age );
}
public abstract void teach();
}
//定义运动员
public abstract class Sopter extends Person{
public Sopter(){
}
public Sopter (String name , int age ){
super (name , age );
}
public abstract void study();
}
//写一个接口
public interface SpeakLunguage{
public abstract void speak();
}
//写篮球教练
public class BasMuster extends Muster{
public BasMuster(){}
public BasMuster(String name , int age ){
}
public void teach(){
System.out.println("传授打篮球技巧");
}
public void eat(){
System.out.println("篮球教练吃牛排");
}
public voidsleep(){
System.out.println("篮球教练三点睡觉");
}
}
//篮球运动员
public class BasSopter extends Sopter implements SpeakLunguage {
public BasSopter(String name,int age) {
super (name,age);
}
public void study() {
System.out.println("打篮球");
}
public void eat (){
System.out.println("泡面加个蛋");
}
public void sleep(){
System.out.println("每天睡得很爽");
}
public void speak() {
System.out.println("说中国话");
}
}
//乒乓球教练
public class PingBangMuster extends Muster implements SpeakLunguage {
public PingBangMuster(){
}
public PingBangMuster (String name ,int age) {
super (name ,age);
}
@Override
public void teach() {
System.out.println(".....");
}
@Override
public void eat() {
System.out.println("吃乒乓球");
}
@Override
public void sleep() {
System.out.println("不睡觉");
}
@Override
public void speak() {
// TODO Auto-generated method stub
}
}
//乒乓球运动员
public class PingBangSopter extends Sopter implements SpeakLunguage {
public PingBangSopter(String name,int age) {
super (name,age);
}
public void study() {
System.out.println("打乒乓球");
}
public void eat (){
System.out.println("泡面没有蛋");
}
public void sleep(){
System.out.println("睡得很爽");
}
public void speak() {
System.out.println("说中国话");
}
}
//测试
public static void main(String[] args) {
//测试篮球教练
Muster m = new BasMuster(“白旭”,38);
String name = m.getName();
int age = m.getAge();
m.sleep();
m.eat();
m.teach();
System.out.println(name +"—" + age);
//测试乒乓球运动员
Sopter s = new PingBangSopter(name, age);
s.setName(“文翰”);
s.setAge(18);
System.out.println(s.getName() + “—” + s.getAge());
s.sleep();
s.eat();
s.study();
//父类不能使用子类特有功能
//s,speak();
//解决方式 向下转型
/*
* PingBangSopter = (PingBangSopter)
*/
//方式2 使用接口的多态
SpeakLunguage sl = new PingBangMuster();
sl.speak();
}
}
**第一次写 见谅
上一篇: Pytorch--ResNet识别MNIST数据集
下一篇: 【golog】我的go学与思2