1.请教老师和各位同学:一下两个问题;
package com.imooc.tel; public class Camera implements IPhoto { @Override public void photo() { // TODO Auto-generated method stub System.out.println("相机有拍照的功能"); } } package com.imooc.tel; public abstract class Computer implements INet { //1.问题1这里为何不加abstract就会报错? @Override public void network() { // TODO Auto-generated method stub System.out.println("电脑有上网的功能"); } @Override public void web() { System.out.println("电脑有了链接WIFI的功能"); } } package com.imooc.tel; //不断的去继承上一个; public class FourhPhone extends ThirdPhone implements IPhoto{ /*public void ptoto() { System.out.println("手机有了拍照的功能"); }*/ public void network() { System.out.println("手机有了上网的功能"); } @Override public void photo() { // TODO Auto-generated method stub System.out.println("手机有了拍照的功能"); } } package com.imooc.tel; public interface INet { /* * 接口中抽象方法可以不写abstract关键字; * 接口访问修饰符 默认 public; * 当类实现接口时,需要去实现类中所有的抽象方法,否则需要将该类设置为抽象类; */ public void network(); //接口中可以包含常量,默认添加public static final关键字; public static final int temp=6; int temp1=20; void web(); } package com.imooc.tel; /** * 具有照相能力的接口; */ public interface IPhoto { public void photo(); } package com.imooc.tel; public class SecondPhone extends Telephone{ public void message() { System.out.println("手机有了发短信的功能"); } } package com.imooc.tel; public class SmartWatch implements INet { public static final int temp=16; @Override public void network() { // TODO Auto-generated method stub System.out.println("电脑有上网的功能"); } public void web() { System.out.println("电脑有了链接WIFI的功能"); } public void Web() { // TODO Auto-generated method stub } } package com.imooc.tel; public class Telephone { private String brand; private int price; public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } public void call() { System.out.println("手机有打电话的功能"); } public Telephone() { } } package com.imooc.tel; public class ThirdPhone extends SecondPhone{ public void video() { System.out.println("手机有了看视频的功能"); } public void music() { System.out.println("手机有了听英语的功能"); } public void game() { System.out.println("手机有了玩游戏的功能"); } }
测试类:package com.imooc.test; import com.imooc.tel.*; public class PhoneTest { public static void main(String[] args) { // TODO Auto-generated method stub FourhPhone one = new FourhPhone(); one.call(); one.game(); one.message(); one.music(); one.network(); one.photo(); one.video(); System.out.println("****************"); IPhoto ip = new FourhPhone(); ip.photo(); ip = new Camera(); ip.photo(); /* * INet two = (INet) new FourhPhone(); two.Web(); */ // 问题二:此处这样写为什么编译出错,是因为找不到对象吗? System.out.println(INet.temp); System.out.println(INet.temp1); INet net = new SmartWatch(); System.out.println(net.temp); SmartWatch three =new SmartWatch(); System.out.println(three.temp); SmartWatch four =new SmartWatch(); System.out.println(four.temp); } }
21
收起
正在回答 回答被采纳积分+1
2回答
Ironxi_work
2018-01-24 21:57:42
问题:为何这样写出错(一个在Computer类中,一个在测试类中)?
Java零基础入门18
- 参与学习 人
- 提交作业 7317 份
- 解答问题 14452 个
想要入门学编程?多年一直活跃在编程语言排行版前列的Java是一个很好的选择。本路径将从Java基础语法、面向对象、常用工具类三部分,为你开启软件开发的大门!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星