• 电子宠物示例


      效果如下图:

    public abstract class Pet {  //父类
    		private String name;
    		private int age;
    		private String sex;
    		private int cute;  //可爱度
    		private int clean; //清洁度
    		private int sleep; //睡眠度
    		private boolean live; //活着
    		
    		public Pet() {
    			super();
    			}
    
    		public Pet(String name,String sex) {
    			super();
    			this.name = name;
    			this.age = 1;
    			this.sex = sex;
    			this.cute = 5;  //初始值
    			this.clean = 50;
    			this.sleep = 50;
    			this.live = true;
    		}
    		public abstract void cutey ();
    		public abstract void cleany();
    			
    		
    		public abstract void sleepy() ;
    			
    		
    		public abstract void excuse();
    		public void show() {  //显示信息的方法
    			System.out.println("名字"+this.name);
    			System.out.println("年龄"+this.age);
    			System.out.println("性别"+this.sex);
    			System.out.println("可爱度"+this.cute);
    			System.out.println("干净度"+this.clean);
    			System.out.println("睡眠度"+this.sleep);
    		}
    		public boolean check() {  //有返回值的方法
    			if(this.clean<=20) {
    				System.out.println("该洗澡了");
    			}
    			if(this.sleep ==30) {
    				System.out.println("休息一下");
    			}else if(this.sleep ==10) {
    				live = false;
    				System.out.println("over");
    			}
    			return live;
    		}
    		
    		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 String getSex() {
    			return sex;
    		}
    
    		public void setSex(String sex) {
    			this.sex = sex;
    		}
    
    		public int getCute() {
    			return cute;
    		}
    
    		public void setCute(int cute) {
    			this.cute = cute;
    		}
    
    		public int getClean() {
    			return clean;
    		}
    
    		public void setClean(int clean) {
    			this.clean = clean;
    		}
    
    		public int getSleep() {
    			return sleep;
    		}
    
    		public void setSleep(int sleep) {
    			this.sleep = sleep;
    		}
    
    		public boolean getLive() {  //注意这个布尔值,如果是自动生成的话,会少一个get,需要自己添一下
    			return live;
    		}
    
    		public void setLive(boolean live) {
    			this.live = live;
    		}	
    		
    			
    }		
    
    public class Dog extends Pet {  //子类
    	
    	public Dog() {
    		super();
    	}	
    	public Dog(String name, String sex) {
    		super(name, sex);
    		
    	}
    
    @Override
    	public void excuse() {
    		System.out.println("忠诚于您");
    		
    	}
    	@Override
    	public void cutey() {
    		System.out.println(super.getName()+"摇尾巴 ,可爱度+5");
    		super.setCute(getCute()+5);
    		
    	}
    
    	
    
    	@Override
    	public void cleany() {
    		System.out.println(super.getName()+"在泥巴里玩,清洁度-10,");
    		super.setClean(getClean()-10);
    
    	}
    
    	@Override
    	public void sleepy() {
    		System.out.println(super.getName()+"趴在地上,睡眠度-10");
    		super.setSleep(getSleep()-10);
    	}
    
    }
    
    public class Cat extends Pet {  //子类
    
    	
    	
    	public Cat() {
    		
    	}	
    	
    
    	public Cat(String name, String sex) {
    		super(name, sex);
    	}	
    	
    
    	@Override
    	public void excuse() {
    		System.out.println("是我的永远是我的");
    		
    	}
    
    	@Override
    	public void cutey() {
    		System.out.println(super.getName()+"玩毛线 ,可爱度+5");
    		super.setCute(getCute()+5);
    
    	}
    
    	@Override
    	public void cleany() {
    		System.out.println(super.getName()+"爬树,清洁度-10");
    		super.setClean(getClean()-10);
    
    	}
    
    	@Override
    	public void sleepy() {
    		System.out.println(super.getName()+"睡在树上,睡眠度-10");
    		super.setSleep(getSleep()-10);
    	}
    
    }
    
    import java.util.Scanner;
    
    import com.hanqi.test.model.Cat;
    import com.hanqi.test.model.Dog;
    import com.hanqi.test.model.Pet;   //引入是因为Test是与Cat,Dog,Pet在不同的包下面
    
    public class Test {  //输出
    
    	public static void main(String[] args) {
    		Scanner scanner = new Scanner(System.in);
    			Pet p = null;  //作用是不用再去实例化好多次
    			System.out.println("选择类型:");
    			System.out.println("1.泰迪  2.黑猫");
    			String s = scanner.nextLine();
    			System.out.println("起个名字吧");
    			String m =scanner.nextLine();
    			System.out.println("雄or雌");
    			String x = scanner.nextLine();
    			if("1".equals(s)) {
    				 p = new Dog(m,x);  //父类的引用指向子类
    				p.excuse();  //打招呼
    			}else {
    				  p =new Cat(m,x);
    				p.excuse();
    			}
    			boolean f = true;
    			while(f) {
    				f = p.check();
    				System.out.println("请选择:0.显示,1.可爱模式 ,2.玩的地点,3.睡的地方 ,4.退出");
    				String w = scanner.nextLine();
    				if("0".equals(w)) {
    					p.show();
    					f = p.check();
    				} else if ("1".equals(w)) {
    					p.cutey();
    					f = p.check();
    				}else if ("2".equals(w)) {
    					p.cleany();
    					f = p.check();
    				}else if ("3".equals(w)) {
    					p.sleepy();
    					f = p.check();
    				}else if ("4".equals(w)) {
    					f = false;
    				}else {
    					System.out.println("请输入内容");
    				}
    				
    				
    				
    				
    			}
    			scanner.close();
    			
    			
    
    	}
    
    }
    
  • 相关阅读:
    Centos常用命令(四、进程)
    搭建git服务器(Centos7)
    tortoiseGit使用
    docker常用命令
    配置docker阿里云加速器_CentOS7
    Centos常用命令(三、网络配置)
    Centos常用命令(二、任务调度和磁盘管理)
    spring的作用
    什么是spring框架
    get和post的区别
  • 原文地址:https://www.cnblogs.com/zuo72/p/7898292.html
Copyright © 2020-2023  润新知