定义抽象类和接口Pet和CanCry, 再定义Dog和Cat分别同时继承Pet和实现CanCry,
接口中定义方法scream
Pet中含有抽象方法run, 分别调用在控制台上打印不同的内容
public class Dog extends Pet implements CanCry { @Override public void Scream() { System.out.println("汪汪"); } @Override public void run() { System.out.println("撒腿跑"); } }
public class Cat extends Pet implements CanCry{ @Override public void Scream() { System.out.println("喵喵"); } @Override public void run() { System.out.println("上树"); } }
public abstract class Pet { public abstract void run (); }
public interface CanCry { public void Scream(); }
Pet dog = new Dog(); //父类的引用指向子类 CanCry c = new Dog(); dog.run(); c.Scream();
编写一个计算时间类MyTime, 含有属性时分秒, 声明两个构造方法, 空参和带参数的,
可以分别实例化一个默认的时间0:0:0和一个指定的时间,
再提供可以分别修改时分秒的三个方法,
可以修改时间为某个时间段之前或之后的时间
public class MyTime { private int hour; private int minutes; private int seconds; public MyTime() {} public MyTime(int hour, int minutes, int seconds) { super(); this.hour = hour; this.minutes = minutes; this.seconds = seconds; } public void changeMinute(int a) { this.setMinutes(this.getMinutes()+a); } public void changeHour(int b) { this.setHour(this.getHour()+b); } public void changeSecond(int c) { this.setMinutes(this.getSeconds()+c); } public int getHour() { return hour; } public void setHour(int hour) { this.hour = hour; } public int getMinutes() { return minutes; } public void setMinutes(int minutes) { this.minutes = minutes; } public int getSeconds() { return seconds; } public void setSeconds(int seconds) { this.seconds = seconds; } @Override public String toString() { return "当前时间: [" + hour + ":" + minutes + ":" + seconds + "]"; }//重写,返回的是字符串 }
MyTime time2 = new MyTime(11,25,20); System.out.println(time2);//系统会默认执行ToString的方法 time2.changeHour(5); time2.changeMinute(10); time2.changeSecond(20); System.out.println(time2);
效果如下图:
编写父类Animals, 子类Dog, Cat, 统计实例化的数量,
即总共有多少只, Dog多少只, Cat多少只, 某一只动物是第几只
public class Animals { //父类 private String name; public static int num; public Animals() {} public Animals(String name) { num++; //为了让其显示动物的个数 this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
public class Dog extends Animals { //子类 public static int dogNum; private int y; public Dog() {} public Dog( String name) { super(name); dogNum++; setY(dogNum); } public static int getDogNum() { return dogNum; } public static void setDogNum(int dogNum) { Dog.dogNum = dogNum; } public int getY() { return y; } public void setY(int y) { this.y = y; } }
public class Cat extends Animals { //子类 public static int catNum; private int x; //给其一个属性接收catNum,同时set,get一下,同理在Dog里也可以实现 public Cat() {} public Cat( String name) { super(name); catNum++; setX(catNum); } public static int getCatNum() { return catNum; } public static void setCatNum(int catNum) { Cat.catNum = catNum; } public int getX() { return x; } public void setX(int x) { this.x = x; } }
Dog a1 = new Dog("小花"); Cat a2 = new Cat("小白"); Dog a3 = new Dog("小黑"); Cat a4 = new Cat("小壮"); System.out.println("总共有"+Animals.num+"只动物"); System.out.println("总共有"+Dog.dogNum + "只狗"+","+Cat.catNum+"只猫"); System.out.println(a1.getName()+"是第"+a1.getY()+"只狗"); System.out.println(a4.getName()+"是第"+a4.getX()+"只猫");
编写一个平面几何图形Shape抽象类, 提供计算面积的方法, 再定义Rectangle矩形类和Circle类, 分别继承
Shape类, 使用不同的方法计算面积
public abstract class Shape { //抽象类 public abstract void getarea() ; }
public class Circle extends Shape { //继承 private int radious; public Circle() {} public Circle(int radious) { super(); this.radious = radious; } @Override public void getarea() { double s = Math.PI*radious*radious; System.out.println("圆的面积" + s); } }
public class Rectangle extends Shape {//继承 private int kuan; private int chang; public Rectangle() {} public Rectangle(int kuan, int chang) { super(); this.kuan = kuan; this.chang = chang; } @Override public void getarea() { double s = kuan*chang; System.out.println("矩形的面积"+ s); } }
Shape x = new Circle(5); Shape y = new Rectangle(5,7); x.getarea(); y.getarea();
猜数字, 编写一个类, 实例化的时候会初始化某个属性, 给一个1-10的随机数,
使用Scanner输入猜数字是多少
出现效果如下图所示:
try catch 捕获异常的体现:
import java.util.Random; public class GussNumber { private int num; //初始化 public GussNumber() { //构造的方法 System.out.println("请输入数字..."); this.setNum(getRandom());//默认的数字就是随机数取出来的数字 } public int getRandom() { //给了一个有返回值的方法 Random r = new Random(); int i = r.nextInt(10)+1; //取1到10的随机数 return i; } public boolean isRight(int num) { //给一个布尔值的又返回类型的方法,判断 boolean f= false; if( this.num == num) { System.out.println("猜对了"); f = true; }else if (this.num > num) { System.out.println("小了"); }else if (this.num < num) { System.out.println("大了"); } return f; }//输对了返回一个true,输错了返回一个false public int getNum() { return num; } public void setNum(int num) { this.num = num; } }
import java.util.Scanner; public class Main { public static void main(String[] args) { GussNumber gn = new GussNumber(); Scanner scanner = new Scanner(System.in); boolean flag = true; while(flag) { //while循环的目的是,如果数字输错了的话,可以继续输入 String s = scanner.nextLine(); //等着输入 try { int x = Integer.parseInt(s);//使输入的内容强转成int型 flag = !gn.isRight(x);//判断输入的内容是否正确,用!的意思是说,如果 是true(输入正确)的话 //,取反,是false,程序不再运行 } catch (NumberFormatException e) { System.out.println("请输入数字.."); } //用try,catch的目的是捕获异常,如果输入的不是数字的话,就会报错 } scanner.close(); //控制台关闭 } }