接口作业的笔记之简答题

以下代码加上了我的想法测试,合理的都能完美运行


二·
1.




public interface Utility{}
class Phone implements Utility{
 void use(){
 System.out.println("using phone");
 }
}
public class Test{
 public static void main(String[] args){
 Utility util=new Phone();
 util.use();//问题所在,就和父类类型变量直接引用子类特有方法一样。//不能用强制类型转换,这个是接口不是父类
 }
}
copy的笔记:
父类类型的引用可以调用父类中定义的所有属性和方法,而对于子类中定义而父类中没有的方法,它是无可奈何的;
同时,父类中的一个方法只有在在父类中定义而在子类中没有重写的情况下,才可以被父类类型的引用调用;
对于父类中定义的方法,如果子类中重写了该方法,那么父类类型的引用将会调用子类中的这个方法,这就是动态连接。
更改A:
public interface Utility{}
class Phone implements Utility{
 void use(){
 System.out.println("using phone");
 }
}
public class Test{
 public static void main(String[] args){
 Phone util=new Phone();//最简单的改法
 util.use();
 }
}
更改B:
public interface Utility{
  void use();//在接口中加了一个方法use,但是有个问题,我把文件复制粘贴过去,报错,说是接口应该公开,但是我的确公开了,我的电脑坏了吗?
//报错原因,一个java文件只能有一个公开类。意味着我要把他们分开放。


}
class Phone implements Utility{
    public void use(){//把包权限改成公开权限
 System.out.println("using phone");
 }
}
public class Test{
 public static void main(String[] args){
 Utility util=new Phone();


 util.use();
 }
}




2. 阅读如下 Java 代码,指出其中存在的错误,并说明错误原因。
public interface Constants{
 int MAX=10000;
 int MIN=1;
}
public class Test{
 public static void main(String[] args){
 Constants con=new Constants();//不能实例化的抽象类
 System.out.println(con.MAX);//不能用实例化调用属性
 int i=50;
 if(i>Constants.MAX){//不能用接口名调用属性
 Constants.MAX=i;//全局静态常量属性不能更改
 }
 }
}






3. 阅读如下 Java 代码,给出运行结果。
public interface Animal{
 void shout();
}
class Dog implements Animal{
 public void shout(){
 System.out.println("W W!");
 }
}
class Cat implements Animal{
 public void shout(){
 System.out.println("M M!");
 }
}
class Store{
 public static Animal get(String choice){
 if(choice.equalsIgnoreCase("dog")){
 return new Dog();
 }else{
 return new Cat();
 }
 }
}
public class AnimalTest{
 public static void main(String[] args){
 Animal al=Store.get("dog");//new Dog();
 al.shout();
 }
}
输出:
W W!




4.在第 3 题基础上进行功能扩展,要求如下。
增加一种新的动物类型:Pig(猪),实现 shout(pig)方法。
修改 Store 类的 get 方法:如果传入的参数是字符串 dog,则返回一个 Dog 对象。
如果传入的参数是字符串 pig,则返回一个 Pig 对象;否则,返回一个 Cat 对象。
在测试类 Test 中加以测试:向 Store 的 get 方法中传入参数"pig",并在返回的
对象上调用 shout 方法,看看与预期的结果是否一致。


public interface Animal {

void shout();



}
class Dog implements Animal{
public void shout(){
System.out.println("W W!");
}
}
package Test;


class Pig implements Animal
{
public void shout(){
System.out.println("夭寿啦杀猪啦!");
}
}


package Test;


class Cat implements Animal{
public void shout(){
System.out.println("M M!");
}
}




package Test;


class Store{
public static Animal get(String choice){
if(choice.equalsIgnoreCase("dog")){
return new Dog();
}else if(choice.equalsIgnoreCase("pig")){
return new Pig();
 
}else{

return new Cat();
}
}
}




package Test;


public class AnimalTest{
public static void main(String[] args){
Animal al=Store.get("dog");//new Dog();
al.shout();
Animal a2 =Store.get("pig");
a2.shout();
}
}






5. 对贯穿案例电子宠物系统的类结构进行重构,要求如下。
? 定义 Eatable 接口,在接口中定义 eat()方法,表示吃饭功能。
? 定义 FlyingDiscCatchable 接口,在接口定义 catchingFlyDisc()方法,表示
玩飞盘功能。
? 定义 Swimmable 接口,在接口中定义 swim()方法,表示游泳功能。
? 定义抽象类 Pet,包括宠物名称 name、健康值 health 和与主人亲密度 love 属性,
并提供抽象方法 print(),用来输出宠物信息。
? 定义狗类 Dog,继承 Pet 类,实现 Eatable、FlyingDiscCatchable 接口,并重
写或实现各个方法。
? 定义企鹅类 Penguin 和继承类 Pet,实现 Eatable 和 Swimmable 接口。并重写或
实现各个方法。
? 编写测试类,实现狗吃饭、企鹅游泳和狗玩接飞盘游戏的功能,并输出企鹅信息。








public interface Eatable {
void eat();


}




public interface FlyingDiscCatchable {
void catchingFlyDisc();


}




public interface Swimmable {
void swim();


}






public abstract class Pet {
String name;
int health;
int love;
public Pet(String name,int health,int love){
this.name = name;
this.health = health;
this.love = love;


}
abstract void print();//注意定义抽象方法没有方法体,并且格式是abstract +返回值+方法名
 




}




class Dog extends Pet implements Eatable,FlyingDiscCatchable {
public Dog(String name,int health,int love){
super(name,health,love);
}
public void print()
{
System.out.println("我是一只狗,名字是"+name+"健康值是"+health+"亲密度是"+love);

}
public void eat(){

System.out.println("吃骨头");
}
public void catchingFlyDisc(){
System.out.println("捡球");

}


}






class Penguin extends Pet implements Eatable,Swimmable{
public Penguin(String name,int health,int love){
super(name,health,love);
}
public void print()
{
System.out.println("我是一只企鹅,名字是"+name+"健康值是"+health+"亲密度是"+love);
}
public void eat(){
System.out.println("吃鱼");
}
public void swim()
{
System.out.println("游泳"); 
}
 
 


}






public class Test {
public static void main(String[] args){
Dog dog = new Dog("旺财",80,90);
dog.print();
dog.eat();
dog.catchingFlyDisc();
Penguin penguin = new Penguin("萌萌",90,100);
penguin.print();
penguin.eat();
penguin.swim();


}

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值