导航:首页 > 编程语言 > java接口的练习题

java接口的练习题

发布时间:2023-08-23 13:25:07

A. java语言程序设计两道练习题。谢谢!

第一题有问题:1、创建Person接口(即“人”),它有setData()和getData()方法对“人”属性name、sex和birthday赋值和获得这些属性组成的字符串信息。
问题是:你说要创建一个人(接口),然后里面有方法对人的属性进行赋值?这怎么可能呢,接口是没有成员变量(属性)的,怎么能赋值?接口里只能有常量。

第二题可以答一下:
package pillar;
public class Pillar { private Geometry buttom;
private double height;
public Pillar() {
// TODO Auto-generated constructor stub
}
public Pillar(Geometry button,double height){
this.buttom = button;
this.height = height;
}
public double getVolume(){
return this.buttom.getArea()*height;
}
public Geometry getButtom() {
return buttom;
}
public void setButtom(Geometry buttom) {
this.buttom = buttom;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}

}
------------------------------------------------类分割线---------------------------------------------------------
package pillar;
public interface Geometry { double getArea();
}
------------------------------------------------类分割线---------------------------------------------------------
package pillar;
public class Circle implements Geometry { private double r;
public Circle() {
// TODO Auto-generated constructor stub
}

public Circle(double r) {
this.r = r;
}

public double getArea() { return Math.PI*r*r;
}
public double getR() {
return r;
}
public void setR(double r) {
this.r = r;
}

}
------------------------------------------------类分割线---------------------------------------------------------
package pillar;
public class Rectangle implements Geometry { private double width;
private double height;

public Rectangle() {
// TODO Auto-generated constructor stub
}

public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}

public double getArea() { return this.width*this.height;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}

}
------------------------------------------------类分割线---------------------------------------------------------
package pillar;
public class TestPillar {
/** * @param args
*/
public static void main(String[] args) {
Circle c = new Circle(5);
Rectangle r = new Rectangle(3,4);
Pillar p1 = new Pillar(c,6);
Pillar p2 = new Pillar(r,6);
System.out.println("圆的体积:"+p1.getVolume()+"\t矩形的体积:"+p2.getVolume());
}

}

B. java关于接口的编程

classMain{
publicstaticvoidmain(String[]args){
IPersonperson=newPerson();
person.setFood("西红柿炒鸡蛋");
person.eat();
}
}

interfaceIPerson{
voideat();
voidsetFood(Stringfood);
}

classPersonimplementsIPerson{

privateStringfood;

@Override
publicvoideat(){
System.out.println("正在吃"+food);
}

@Override
publicvoidsetFood(Stringfood){
this.food=food;
}
}

C. java编程题:请按照下列提示编写一个泛型接口以及其实现类

Generic.java:
package com.example.demo;

public interface Generic<T> {

void get(T t);
}

GenericImpl.java:

package com.example.demo;

public class GenericImpl<T> implements Generic<T> {

@Override
public void get(T t) {

}
}

D. JAVA接口的一道习题

//抽象类Person

packageA;

publicabstractclassPerson{

Stringname=null;

Stringaddress=null;

publicabstractvoidAddWage();

}

//实现&测试类

packageA;

classManagerextendsEmployee{

Stringlevel;

publicManager(Stringname,Stringaddress,StringID,Doublewage,intworkYear,Stringlevel){

super(name,address,ID,wage,workYear);

this.level=level;

}

publicvoidAddWage(){

wage=wage*1.2;

}

publicStringtoString(){

return"Name:"+name+" "+

"Address:"+address+" "+

"ID:"+ID+" "+

"Wage:"+wage+" "+

"WorkYear:"+workYear+" "+

"ManagerLevel:"+level+" ";

}

}

classEmployeeextendsPerson{

StringID;

Doublewage;

intworkYear;

publicEmployee(Stringname,Stringaddress,StringID,Doublewage,intworkYear){

this.name=name;

this.address=address;

this.ID=ID;

this.wage=wage;

this.workYear=workYear;

}

publicvoidAddWage(){

wage=wage*1.1;

}

publicStringtoString(){

return"Name:"+name+" "+

"Address:"+address+" "+

"ID:"+ID+" "+

"Wage:"+wage+" "+

"WorkYear:"+workYear+" ";

}

}

publicclassTestManager{

publicstaticvoidmain(String[]args){

Employeee=newEmployee("Tony","USA","209881",20000D,4);

Managerm=newManager("Bush","China","209999",100000D,10,"D");

System.out.println(e+"----------- "+m);

}

}

E. java第八章回答接口的问题

//定义具体类AirPlane,继承Machine
//飞机的work() 方法调用时,输出“正在飞行,速度为…”
public class AirPlane extends Machine {

@Override
void work() {
// TODO Auto-generated method stub
System.out.println("正在飞行,速度为…");
}

}

//定义一个接口canFly,其中,含有speed()方法,
public interface CanFly {

void speed();

}
//并实现canFly接口,每个飞机有型号、飞行速度,编写构造方法,toString()方法,
public class CanFlyImp implements CanFly {

private String xinghao;

private int su;

public CanFlyImp() {
super();
// TODO Auto-generated constructor stub
}

public CanFlyImp(String xinghao, int su) {
super();
this.xinghao = xinghao;
this.su = su;
}

public void speed() {
// TODO Auto-generated method stub

}

@Override
public String toString() {
return "CanFlyImp [xinghao=" + xinghao + ", su=" + su + "]";
}

//set get.....

}

//定义一个抽象类Machine,其中含work()方法,
public abstract class Machine {

abstract void work();

}
//创建一个飞机对象测试,执行对象work()方法;
public class Test {

public static void main(String[] args) {
AirPlane ap = new AirPlane();

ap.work();

CanFlyImp canFlyImp = new CanFlyImp("002",12 );

System.out.println(canFlyImp);

canFlyImp.speed();
}

}

阅读全文

与java接口的练习题相关的资料

热点内容
如何在服务器上配置外网网址 浏览:838
阿里云服务器的硬件在哪里 浏览:50
python自动注册谷歌 浏览:329
phpini验证码 浏览:824
解压后的文件怎么驱动 浏览:326
老板要程序员加班 浏览:414
泰尔pdf 浏览:311
视频转码压缩哪款软件好 浏览:647
盯盯拍记录仪下载什么app 浏览:436
新东方新概念英语pdf 浏览:696
python中如何创建菜单栏 浏览:507
中石化app那个叫什么名 浏览:706
借贷宝合集解压密码 浏览:640
python爬取网页代码 浏览:480
efs加密对微信无效 浏览:496
刘秀pdf 浏览:998
脚上长黑刺是什么app 浏览:703
算法工程师上海 浏览:390
php的循环语句怎么写 浏览:289
画圣诞树用什么软件python 浏览:452