zoukankan      html  css  js  c++  java
  • 《java入门第一季》之面向对象综合小案例

    需求:

    /*
        教练和运动员案例
            乒乓球运动员和篮球运动员。
            乒乓球教练和篮球教练。
            跟乒乓球相关的人员都需要学习英语。
            分析,这个案例中有哪些抽象类,哪些接口,哪些具体类。
        
        整个分析过程,由图片展示。    
    */


    代码如下:

    interface SpeakEnglish {
    	//说英语
    	public abstract void speak();
    }
    
    //定义人的抽象类
    abstract class Person {
    	private String name;
    	private int age;
    	
    	public Person() {}
    	
    	public Person(String name,int age) {
    		this.name = name;
    		this.age = 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 void sleep() {
    		System.out.println("人都是要睡觉的");
    	}
    	
    	//吃饭
    	public abstract void eat();//吃的不一样,抽象方法
    }
    
    //定义运动员(抽象类)
    abstract class Player extends Person {
    	public Player() {}
    	
    	public Player(String name,int age) {
    		super(name,age);
    	}
    	//这里会继承父类吃饭功能
    	//学习
    	public abstract void study();//运动员学习内容不一样,抽取为抽象
    }
    
    //定义教练(抽象类)
    abstract class Coach extends Person {
    	public Coach() {}
    	
    	public Coach(String name,int age) {
    		super(name,age);
    	}
    	//教
    	public abstract void teach();//教练教的不一样,抽象方法
    }
    
    //乒乓球运动员
    class PingPangPlayer extends Player implements SpeakEnglish {
    	public PingPangPlayer(){}
    	
    	public PingPangPlayer(String name,int age) {
    		super(name,age);
    	}
    	
    	//吃
    	public void eat() {
    		System.out.println("乒乓球运动员吃大白菜,喝小米粥");
    	}
    	
    	//学习
    	public void study() {
    		System.out.println("乒乓球运动员学习如何发球和接球");
    	}
    	
    	//说英语,对于接口抽象方法的具体重写
    	public void speak() {
    		System.out.println("乒乓球运动员说英语");
    	}
    }
    
    //定义篮球运动员具体类
    class BasketballPlayer extends Player {//不需要继承接口,因为他不需要说英语
    	public BasketballPlayer(){}
    	
    	public BasketballPlayer(String name,int age) {
    		super(name,age);
    	}
    	
    	//吃
    	public void eat() {
    		System.out.println("篮球运动员吃牛肉,喝牛奶");
    	}
    	
    	//学习
    	public void study() {
    		System.out.println("篮球运动员学习如何运球和投篮");
    	}
    }
    
    //定义乒乓球教练具体类
    class PingPangCoach extends Coach implements SpeakEnglish {
    	public PingPangCoach(){}
    	
    	public PingPangCoach(String name,int age) {
    		super(name,age);
    	}
    	
    	//吃
    	public void eat() {
    		System.out.println("乒乓球教练吃小白菜,喝大米粥");
    	}
    	
    	//教
    	public void teach() {
    		System.out.println("乒乓球教练教如何发球和接球");
    	}
    	
    	//说英语    对于接口抽象方法的具体重写
    	public void speak() {
    		System.out.println("乒乓球教练说英语");
    	}
    }
    
    //定义篮球教练具体类
    class BasketballCoach extends Coach {
    	public BasketballCoach(){}
    	
    	public BasketballCoach(String name,int age) {
    		super(name,age);
    	}
    	
    	//吃
    	public void eat() {
    		System.out.println("篮球教练吃羊肉,喝羊奶");
    	}
    	
    	//教
    	public void teach() {
    		System.out.println("篮球教练教如何运球和投篮");
    	}
    }
    
    class InterfaceDemo {
    	public static void main(String[] args) {
    		//测试运动员(乒乓球运动员和篮球运动员)
    		//乒乓球运动员
    		PingPangPlayer ppp = new PingPangPlayer();//自己类实现
    		ppp.setName("王浩");
    		ppp.setAge(33);
    		System.out.println(ppp.getName()+"---"+ppp.getAge());
    		ppp.eat();
    		ppp.sleep();
    		ppp.study();
    		ppp.speak();
    		System.out.println("----------------");
    		//通过带参构造给数据
    		ppp = new PingPangPlayer("张继科",27);
    		System.out.println(ppp.getName()+"---"+ppp.getAge());
    		ppp.eat();
    		ppp.sleep();
    		ppp.study();
    		ppp.speak();
    		System.out.println("----------------");
    		
    		//篮球运动员
    		BasketballPlayer bp = new BasketballPlayer();
    		bp.setName("姚明");
    		bp.setAge(34);
    		System.out.println(bp.getName()+"---"+bp.getAge());
    		bp.eat();
    		bp.sleep();
    		bp.study();
    		//bp.speak(); //没有该方法,所以会报错
    		System.out.println("----------------");
    		//通过带参构造给数据
    		bp = new BasketballPlayer("易建联",31);
    		System.out.println(bp.getName()+"----"+bp.getAge());
    		bp.eat();
    		bp.sleep();
    		bp.study();
    		System.out.println("----------------");
    		
    		//乒乓球教练
    		PingPangCoach ppc = new PingPangCoach();
    		ppc.setName("杨道龙");
    		ppc.setAge(24);
    		System.out.println(ppc.getName()+"---"+ppc.getAge());
    		ppc.eat();
    		ppc.sleep();
    		ppc.teach();
    		ppc.speak();
    		System.out.println("----------------");
    		//通过带参构造给数据
    		ppc = new PingPangCoach("老K教练",56);
    		System.out.println(ppc.getName()+"---"+ppc.getAge());
    		ppc.eat();
    		ppc.teach();
    		ppc.sleep();
    		ppc.speak();
    		//篮球教练
    		BasketballCoach bc = new BasketballCoach();
    		bc.setName("杨道龙");
    		bc.setAge(24);
    		System.out.println(bc.getName()+"---"+bc.getAge());
    		bc.eat();
    		bc.sleep();
    		bc.teach();
    		//bc.speak();
    		bc = new BasketballCoach("李继超",34);
    		System.out.println(bc.getName()+"---"+bc.getAge());
    		bc.eat();
    		bc.sleep();
    		bc.teach();
    		System.out.println("----------------");
    	}
    }


  • 相关阅读:
    linux异常文件下载
    spring boot项目发布到tomcat端口问题
    jenkins.service changed on disk. Run 'systemctl daemon-reload' to reload units
    Job for jenkins.service failed because the control process exited with error code
    安装配置jenkins rpm方式
    Error creating bean with name 'eurekaAutoServiceRegistration': Singleton bean creation not allowed w
    Neither the JAVA_HOME nor the JRE_HOME environment variable is defined At least one of these environ
    Cannot find class: org.mybatis.caches.ehcache.LoggingEhcache
    java.lang.IllegalArgumentException: invalid comparison: java.util.Date and java.lang.String
    Error:(12, 8) java: 无法访问javax.servlet.ServletException 找不到javax.servlet.ServletException的类文件
  • 原文地址:https://www.cnblogs.com/wanghang/p/6299849.html
Copyright © 2011-2022 走看看