第一题:
答案:
class Animal{//成员变量protected String name;protected int weight;//构造方法public Animal(){this.name="refer";this.weight=50;}public Animal(String name,int weight){this.name=name;this.weight=weight;}//成员方法public void show(){System.out.println("名字:"+this.name+" 体重:"+this.weight);}
}
class Dog extends Animal{//成员变量private String color;//构造方法public Dog(String name,int weight){super(name,weight); //实例化的时候要先初始化父类color="yellow";}//成员方法public void show(){ //重写,返回值类型,方法名,形参全部相同,且子类访问限制符要大于等于父类访问限制符System.out.println("名字:"+this.name+" 体重:"+this.weight+" 颜色:"+this.color);}
}
public class P7_1 {public static void main(String[] args) {Dog dog=new Dog("refer",50);dog.show();}
}
第二题:
答案:
interface IPay{double pay(); //符合阿里编码规范,就不再加public abstract,提高代码简洁性
}
class Winter implements IPay{int elc=145; //默认是public static finaldouble money=0.49;public double pay(){ //实现接口中的抽象方法,且不能使用默认修饰符,必须使用publicreturn elc*money;}
}
class Summer implements IPay{int elc=125;double money=0.49;public double pay(){return elc*money;}
}
class PayMoney{public void season(IPay pay){if (pay instanceof Winter){ //如果是冬天Winter winter=new Winter();System.out.println("冬天:"+winter.pay());}else if (pay instanceof Summer){ //如果是夏天Summer summer=new Summer();System.out.println("夏天:"+summer.pay());}else { //如果既不是冬天也不是夏天System.out.println("请传入正确的参数");}}
}
public class P7_1 {public static void main(String[] args) {PayMoney p=new PayMoney();p.season(new Winter()); //使用匿名对象p.season(new Summer()); //使用匿名对象}
}
第三题:
答案:
interface Animal{String address="广州动物园";default void eat(){ //接口里default和static修饰的方法可以进行实现System.out.println("每天吃得饱饱的!");}static void show(){System.out.println("我是动物!");}void ability(); //省略pubic abstract,提高代码简洁性
}
class Bird implements Animal{public Bird(){System.out.println("我是广州动物园的鸟。");}@Override //添加注解,利用编译器的检查优势,提高开发效率public void eat(){ //重写方法必须用public修饰,不能使用默认的访问权限Animal.show();System.out.println("我爱吃虫子!每天吃得饱饱的!");}@Overridepublic void ability() {System.out.println("我会飞!");}
}
class Fish implements Animal{public Fish(){System.out.println("我是广州动物园的鱼。");}@Overridepublic void eat(){Animal.show();System.out.println("我爱吃小虾!每天吃得饱饱的!");}@Overridepublic void ability() {System.out.println("我会游泳!");}
}
class Monkey implements Animal{public Monkey(){System.out.println("我是广州动物园的猴子。");}@Overridepublic void eat(){Animal.show();System.out.println("我爱吃香蕉!每天都吃得饱饱的!");}@Overridepublic void ability() {System.out.println("我会爬树!");}
}
public class Test {public static void main(String[] args) {Bird bird=new Bird();bird.eat();bird.ability();Fish fish=new Fish();fish.eat();fish.ability();Monkey monkey=new Monkey();monkey.eat();monkey.ability();}
}
第四题:
答案:
第五题:
答案:
enum ReviewPlan{//枚举成员必须放在类的一开始,使用enum来枚举就必须省略public static final,如果使用自定义枚举类则规则不一样Monday("星期一","数学"),Tuesday("星期二","数据结构"),Wednesday("星期三","Java"),Thursday("星期四","日语"),Friday("星期五","离散数学"),Saturday("星期六","Html+Css+JavaScript"),Sunday("星期天","电影艺术");private final String day; //因为枚举针对的是有限个且确定的对象,不希望被修改,所以用private finalprivate final String plan;private ReviewPlan(String day,String plan){ //私有化类的构造器this.day=day;this.plan=plan;}public String toString(){ //重写enum里自带的toString方法return this.day+this.plan;}
}
public class Test {public static void main(String[] args) {for (ReviewPlan r:ReviewPlan.values()){System.out.println(r.name()+"的属性是:"+r.toString());}}
}