定义一个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;//
protected int sex;//
protected int age;
protected int happy;//
protected int healthy;//
protected int hungry;//
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 .玩游戏");
}
}

最新文章

  1. Python标准模块--ContextManager
  2. Spring拦截机制之后端国际化心得
  3. Count the number of possible triangles
  4. Android入门(四)UI-创建自定义控件
  5. javascript里面this机制的几个例子
  6. BZOJ3873 : [Ahoi2014]拼图
  7. Fresco 源码分析(二) Fresco客户端与服务端交互(1) 解决遗留的Q1问题
  8. 我30天在Stack Overflow问答网站上回答问题的感受
  9. 纯CSS实现三列布局(两边固定,中间自适应)
  10. Windows10 Enterprise版本周年更新问题
  11. 安装Cnario Player 3.8.1.156或其他版本时提示&quot;Warning 4154. Adobe Flash Player 13 ...not correctly installed&quot;
  12. Markdown基础语法笔记
  13. jdbc中的sql注入
  14. leetcode — jump-game-ii
  15. Ubuntu 16.04交换Ctrl和Caps
  16. JDK Timer &amp; TimerTask
  17. Extjs gridpanel 合并单元格
  18. MVC之Filter
  19. 死磕salt系列-salt配置文件
  20. Swift中UIView类方法(animateWithDuration)的使用

热门文章

  1. 基于cookie使用过滤器实现客户每次访问自登陆一次
  2. Java语言编程注意事项
  3. java书系列之——前言
  4. EF Core学习Code First
  5. javamail 邮件格式再优化(由详情——&gt;改为统计)
  6. Bash shell执行命令的优先级
  7. 【收藏】socket 中的 recv与send函数
  8. springmvc 之 helloworld
  9. java加密算法入门(三)-非对称加密详解
  10. rem的js