導航:首頁 > 編程語言 > 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介面的練習題相關的資料

熱點內容
女程序員寫的小說 瀏覽:774
華為路由器ip設置命令 瀏覽:552
如何打開軟體伺服器 瀏覽:756
單片機介面技術及應用 瀏覽:751
linux下執行腳本文件 瀏覽:127
撥號加密保護 瀏覽:435
b站彈幕加密 瀏覽:601
交友盲盒源碼破解 瀏覽:248
單片機100位百位符號 瀏覽:686
用友通加密狗壞了 瀏覽:550
如何在伺服器上配置外網網址 瀏覽:843
阿里雲伺服器的硬體在哪裡 瀏覽:54
python自動注冊谷歌 瀏覽:329
phpini驗證碼 瀏覽:826
解壓後的文件怎麼驅動 瀏覽:328
老闆要程序員加班 瀏覽:416
泰爾pdf 瀏覽:313
視頻轉碼壓縮哪款軟體好 瀏覽:649
盯盯拍記錄儀下載什麼app 瀏覽:438
新東方新概念英語pdf 瀏覽:698