Java之面向对象例子(二)
定义一个Book类,在定义一个JavaBook类继承他
//book类 package com.hanqi.maya.model; public class Book { public String name; public int no; public Book(){ } public Book(String name1,int no1){ name =name1; no=no1; } public String show(){ return "书名是:"+name+","+"页数:"+no; } }
//javaBook类继承 package com.hanqi.maya.model; public class JavaBook extends Book{ public String nandu; public JavaBook(){ } public JavaBook(String nandu1){ nandu=nandu1; } public String show(){ String str=super.show(); str+="难度是"+nandu; return str; } }
//主函数 package com.hanqi.maya.main; import com.hanqi.maya.model.JavaBook; public class Main { public static void main(String[] args){ JavaBook j=new JavaBook("21"); j.name="21天"; j.nandu="初级"; j.no=20; String s=j.show(); System.out.println(s); } }
super关键字。和this相对,对父类对象的引用
子类继承父类,子类的构造方法必须调用父类的构造方法,动用哪一个都行,如果子类没有去调用,子类会默认调用父类的空参构造方法,这个时候父类中如果没有空参构造方法,会报错
//父类 package com.hanqi.maya.model; public class Book { public String name; public int no; public Book(){ } public Book(String name1,int no1){ name =name1; no=no1; } public String show(){ return "书名是:"+name+","+"页数:"+no; } }
//子类 package com.hanqi.maya.model; public class JavaBook extends Book{ public String nandu; public JavaBook(){ } public JavaBook(String name1,int nol,String nandu1){ //父类对象的引用,这一行代表调用了父类构造函数 //需要注意这里的括号里的参数,和方法里的内容,他们的类型的顺序要是一样的,和后面实例化传入的参数的顺序也要是一样的 //子类继承父类,子类的构造方法必须调用父类的构造方法,动用哪一个都行,如果子类没有去调用,子类会默认调用父类的空参构造方法,这个时候父类中如果没有空参构造方法,会报错 super(name1,nol); nandu=nandu1; } public String show(){ String str=super.show(); str+="难度是"+nandu; return str; } }
//主方法 package com.hanqi.maya.main; import com.hanqi.maya.model.JavaBook; public class Main { public static void main(String[] args){ //注意这里的类型顺序要和构造方法中一样 JavaBook j=new JavaBook("21",20,"初级"); /*j.name="21天"; j.nandu="初级"; j.no=20; */ String s=j.show(); System.out.println(s); } }
this关键字
this代表类本身
package com.hanqi.maya.model; //book类 public class Book { public String name; public int no; public Book(){ } public Book(String name,int no){ //这个类里的name this.name=name; this.no=no; } /*public Book(String name1,int no1){ name =name1; no=no1; }*/ public String show(){ return "书名是:"+name+","+"页数:"+no; } }
//JavaBook子类 package com.hanqi.maya.model; public class JavaBook extends Book{ public String nandu; public JavaBook(){ } public JavaBook(String name1,int nol,String nandu1){ //父类对象的引用,这一行代表调用了父类构造函数 //需要注意这里的括号里的参数,和方法里的内容,他们的类型的顺序要是一样的,和后面实例化传入的参数的顺序也要是一样的 //子类继承父类,子类的构造方法会默认调用父类的构造方法,,子类会默认加一个super(), super(name1,nol); nandu=nandu1; } public String show(){ String str=super.show(); str+="难度是"+nandu; return str; } }
宠物栗子,可以显示,取名,玩耍,喂食,显示信息
//宠物类 父类 package com.hanqi.maya.model; import java.util.Scanner; public class Pet { public String lx; protected String name;//1 protected int sex;//1 protected int age; protected int happy;//80 protected int healthy;//100 protected int hungry;//80 public static int uName; public Pet(){ } public Pet(int sex){ this.sex=sex; this.age=1; this.happy=80; this.healthy=100; this.hungry=80; } public void playGame(){ if(!check()){ System.out.println("各项属性值不能为负数"); return; } System.out.println(this.name+"正在和你玩耍"); this.happy+=10; this.healthy-=5; this.hungry+=12; } /*public void quName(){ Scanner sca=new Scanner(System.in); String s = sca.nextLine(); this.name=s; }*/ public void eat(){ if(!check()){ System.out.println("各项属性值不能为负数"); return; } System.out.println(this.name+"正在吃饭"); this.healthy+=5; this.hungry-=20; } public boolean check(){ if(this.happy>=0&&this.healthy>=0&&this.hungry>=0){ return true; } if(happy<0){ happy=0; } if(healthy<0){ healthy=0; } if(hungry<0){ hungry=0; } return false; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getSex() { return sex; } public void setSex(int sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public int getHappy() { return happy; } public void setHappy(int happy) { this.happy = happy; } public int getHealthy() { return healthy; } public void setHealthy(int healthy) { this.healthy = healthy; } public int getHungry() { return hungry; } public void setHungry(int hungry) { this.hungry = hungry; } }
//猫类 子类 package com.hanqi.maya.model; public class Cat extends Pet{ public Cat(){ } public Cat(int catSex){ super(catSex); } public void show(){ System.out.println("名称:"+this.name); System.out.println("性别:"+this.sex); System.out.println("年龄:"+this.age); System.out.println("开心值:"+this.happy); System.out.println("健康值:"+this.healthy); System.out.println("的饥饿值:"+this.hungry); } }
//主方法 package com.hanqi.maya.model; import java.util.Scanner; public class Main { public static void main(String[] args) { Cat cat=new Cat(1); Scanner sc=new Scanner(System.in); boolean flag=true; while(flag){ printControl(); String s=sc.nextLine(); if("0".equals(s)){ String quna=sc.nextLine(); cat.name=quna; }else if("1".equals(s)){ cat.show(); }else if("2".equals(s)){ cat.eat(); }else if("3".equals(s)){ cat.playGame(); }else if("bye".equals(s)){ System.out.println("bye bye"); flag=false; } } sc.close(); } public static void printControl(){ System.out.println("x.选择宠物"); System.out.println("0.给宠物取个名字吧"); System.out.println("1 .显示信息"); System.out.println("2 .吃饭"); System.out.println("3 .玩游戏"); } }