java 初学者 遇到了一道题没有头绪 请大侠们帮忙解答 谢谢。。。。
发布网友
发布时间:2022-05-25 17:33
我来回答
共4个回答
热心网友
时间:2023-11-25 06:06
总共7个类
package Mobile;
public interface Camera {
public void takePhoto();
}
package Mobile;
public interface MobilePhone {
public void call();
public void receive();
public void sendMsg();
public void receiveMsg();
}
package Mobile;
public interface CameraPhone extends MobilePhone, Camera {
}
package Mobile.Impl;
import Mobile.CameraPhone;
public class MotoPhone implements CameraPhone {
public void call() {
System.out.println("Moto call");
}
public void receive() {
System.out.println("Moto println");
}
public void receiveMsg() {
System.out.println("Moto receiveMsg");
}
public void sendMsg() {
System.out.println("Moto sendMsg");
}
public void takePhoto() {
System.out.println("Moto takePhoto");
}
}
package Mobile.Impl;
import Mobile.CameraPhone;
public class NokiaPhone implements CameraPhone {
public void call() {
System.out.println("Nokia call");
}
public void receive() {
System.out.println("Nokia receive");
}
public void receiveMsg() {
System.out.println("Nokia receiveMsg");
}
public void sendMsg() {
System.out.println("Nokia sendMsg");
}
public void takePhoto() {
System.out.println("Nokia takePhoto");
}
}
package Student;
import Mobile.CameraPhone;
/*
* 学生类
* 定义 姓名 手机使用型号
*
* */
public class Student {
public String name ; // 学生姓名
public CameraPhone cPhone ; // 学生手机
public CameraPhone getCPhone() {
return cPhone;
}
public void setCPhone(CameraPhone phone) {
cPhone = phone;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
/*
* 带参数的构造方法
*
* */
public Student(String name , CameraPhone cPhone ){
this.setName(name); // 初始化参数
this.setCPhone(cPhone); // 初始化参数
}
/*
* 拨打电话功能
* */
public void call(){
this.cPhone.call();
}
}
测试类
import Mobile.Impl.MotoPhone;
import Mobile.Impl.NokiaPhone;
import Student.Student;
public class TestInterface {
/**
* @param args
*/
public static void main(String[] args) {
NokiaPhone nokie = new NokiaPhone();
MotoPhone moto = new MotoPhone();
Student student = new Student("张三" , nokie);
student.call();
Student student1= new Student("李四" , moto);
student1.call();
}
}
热心网友
时间:2023-11-25 06:07
package test;
public class TestInterface {
/**
* @param args
*/
public static void main(String[] args) {
Student s1 = new Student("张三",new NokiaPhone());
Student s2 = new Student("李四",new MotoPhone());
s1.myCall();
s2.myCall();
}
}
interface MobilePhone{
void call();
void receive();
void sendMsg();
void receiveMsg();
}
interface Camera{
void takePhoto();
}
interface CameraPhone extends MobilePhone,Camera{
}
class NokiaPhone implements CameraPhone{
@Override
public void call() {
System.out.print("Nokia call");
}
@Override
public void receive() {
System.out.print("Nokia receive");
}
@Override
public void receiveMsg() {
System.out.print("Nokia receiveMsg");
}
@Override
public void sendMsg() {
System.out.print("Nokia sendMsg");
}
@Override
public void takePhoto() {
System.out.print("Nokia takePhoto");
}
}
class MotoPhone implements CameraPhone{
@Override
public void call() {
System.out.print("Moto call");
}
@Override
public void receive() {
System.out.print("Moto receive");
}
@Override
public void receiveMsg() {
System.out.print("Moto receiveMsg");
}
@Override
public void sendMsg() {
System.out.print("Moto sendMsg");
}
@Override
public void takePhoto() {
System.out.print("Moto takePhoto");
}
}
class Student{
private String name;
private CameraPhone myPhone;
public Student(String name,CameraPhone phone){
this.name=name;
this.myPhone=phone;
}
public void myCall(){
this.myPhone.call();
}
}
热心网友
时间:2023-11-25 06:07
public interface MobilePhone {
public void call();
public void receive();
public void sendMsg();
public void receiveMsg();
}
public interface Camera {
public void takePhoto();
}
public class CameraPhone implements MobilePhone, Camera {
@Override
public void call() {
// TODO Auto-generated method stub
System.out.println("call() is called!");
}
@Override
public void receive() {
// TODO Auto-generated method stub
System.out.println("receive() is called!");
}
@Override
public void receiveMsg() {
// TODO Auto-generated method stub
System.out.println("receiveMsg() is called!");
}
@Override
public void sendMsg() {
// TODO Auto-generated method stub
System.out.println("sendMsg() is called!");
}
@Override
public void takePhoto() {
// TODO Auto-generated method stub
System.out.println("takePhoto() is called!");
}
}
public class MotoPhone extends CameraPhone {
@Override
public void call() {
// TODO Auto-generated method stub
System.out.println("MotoPhone call() is called!");
}
@Override
public void receive() {
// TODO Auto-generated method stub
System.out.println("MotoPhone receive() is called!");
}
@Override
public void receiveMsg() {
// TODO Auto-generated method stub
System.out.println("MotoPhone receiveMsg() is called!");
}
@Override
public void sendMsg() {
// TODO Auto-generated method stub
System.out.println("MotoPhone sendMsg() is called!");
}
@Override
public void takePhoto() {
// TODO Auto-generated method stub
System.out.println("MotoPhone takePhoto() is called!");
}
}
public class NokiaPhone extends CameraPhone {
@Override
public void call() {
// TODO Auto-generated method stub
System.out.println("NokiaPhone call() is called!");
}
@Override
public void receive() {
// TODO Auto-generated method stub
System.out.println("NokiaPhone receive() is called!");
}
@Override
public void receiveMsg() {
// TODO Auto-generated method stub
System.out.println("NokiaPhone receiveMsg() is called!");
}
@Override
public void sendMsg() {
// TODO Auto-generated method stub
System.out.println("NokiaPhone sendMsg() is called!");
}
@Override
public void takePhoto() {
// TODO Auto-generated method stub
System.out.println("NokiaPhone takePhoto() is called!");
}
}
public class Student {
private String name;
private CameraPhone myPhone;
public Student(String name,CameraPhone myPhone){
this.name=name;
this.myPhone=myPhone;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public CameraPhone getPhone() {
return myPhone;
}
public void setPhone(CameraPhone myPhone) {
this.myPhone = myPhone;
}
public void myCall(){
this.myPhone.call();
}
}
public class TestInterface {
public static void main(String args[]){
NokiaPhone Nokia=new NokiaPhone();
MotoPhone Moto=new MotoPhone();
Student stu1=new Student("张三",Nokia);
Student stu2=new Student("李四",Moto);
stu1.myCall();
stu2.myCall();
}
}
热心网友
时间:2023-11-25 06:08
interface MobilePhone{
void call();
void receive();
void sendMsg();
void receiveMsg();
}
interface Camera{
void takePhoto();
}
........。。。。
其他的就是这么定义的,要继承该接口要全部实现其方法追问嗯 前面的我会写后面几条就混乱了是不是得用上向上转型呀。。。