導航:首頁 > 編程語言 > java多態習題

java多態習題

發布時間:2022-08-29 06:18:28

java類的多態練習

public class Exam
{
public static void main(String[] args)
{
for(Shape s : new Shape[] {new Rectangle(8,6),new Circle(10)})
{
System.out.println(s.getDescription());
System.out.println(String.format("面積:%.1f",s.getArea()));
}
}
}

abstract class Shape
{
abstract String getDescription();
abstract double getArea();
}

class Rectangle extends Shape
{
Rectangle(double l,double w)
{
length=l;
width=w;
}
public String getDescription()
{
return String.format("長為%.1f,寬為%.1f的長方形!",length,width);
}
public double getArea()
{
return length*width;
}
private double length,width;
}

class Circle extends Shape
{
Circle(double r)
{
radius=r;
}
public String getDescription()
{
return String.format("半徑為%.1f的圓!",radius);
}
public double getArea()
{
return Math.PI*radius*radius;
}
private double radius;
}

❷ java繼承多態的練習題

Java繼承是使用已存在的類的定義作為基礎建立新類的技術,新類的定義可以增加新的數據或新的功能,也可以用父類的功能,但不能選擇性地繼承父類。
java多態存在的三個必要條件:
1.需要有繼承關系的存在
2.需要有方法的重寫
3.需要有父類的引用指向子類對象
希望對你有幫助。
第一題應該選D,第二題選C,D。
第一題屬於多態,methodB()方法屬於子類,父類沒有重寫子類的方法
第二題屬於繼承,子類可以繼承父類的方法

❸ 一道JAVA編程題(繼承與多態)

很有難度啊、題目也比較發散、選擇性太多、太大、真是無從下手啊。。。

❹ java題目,求助,多態。

publicclass多態
{
publicstaticvoidmain(String[]args)
{
System.out.println(" ==========java題目,求助,多態!========== ");
init();
}//初始化!
privatestaticvoidinit()
{
Animalf=newFf("菲菲");
Animalj=newJj("靜靜");
Apa=newAp("阿平",f);

//假設單數雙數為換周,換節目表演的話!
for(inti=1;i<=3;i++)
{
if(i%2!=0)
a.setInstanceof(f);
else
a.setInstanceof(j);
a.train();
a.perform();
System.out.println(" ------------------------------------------------- ");
}
}
}
interfaceAnimal
{
voidtrain();
voidperform();
}

{
F(Stringname){this.name=name;}
protectedStringname;
abstractpublicvoidtrain();
abstractpublicvoidperform();
}
classFfextendsF
{
Ff(Stringname){super(name);}
publicvoidtrain()
{
System.out.println(name+":---->天上飛!");
}
publicvoidperform()
{
System.out.println(name+":--->天空翻轉飛翔");
}
}
classJjextendsF
{
Jj(Stringname){super(name);}
publicvoidtrain()
{
System.out.println(name+":------------------>水裡游");
}
publicvoidperform()
{
System.out.println(name+":----------->水裡跳躍");
}
}
classApextendsF
{
privateAnimala;
Ap(Stringname,Animala){super(name);this.a=a;}
voidsetInstanceof(Animala)
{
this.a=a;
}
publicvoidtrain()
{
if(a==null)return;
System.out.println(name+":訓練動物");
if(ainstanceofFf)
{
Fff=(Ff)a;
f.train();
}else
{
Jjj=(Jj)a;
j.train();
}
}
publicvoidperform()
{
if(a==null)return;

System.out.println("特技展示:");

if(ainstanceofFf)
{
Fff=(Ff)a;
f.perform();
}else
{
Jjj=(Jj)a;
j.perform();
}
}
}

❺ 求java中幾個繼承與多態實例,並要有相應的題目!給我參考和練習下,本人新手,不宜難的

JAVA中一個抽象類抽象方法繼承與對象多態性的例子
面向對象的三大特點:封裝,繼承,多態。

在JAVA中我們總是盡可能地讓一個類繼承一個抽象類,這樣大大的節省代碼方便開發。

一個繼承與對象多態性的例子:聲明一個Person 類。Student 類,Worker類分別繼承Person。

人有姓別,年齡,學生有特有的成績屬性,工人有特有的工資。

所有屬性都用private封裝

abstract class Person{
private String name;
private int age;
public Person(String name,int age){
this.setName(name);
this.setAge(age);
}
public void setName(String name){
this.name=name;
}
public void setAge(int age){
this.age=age;
}
public String getName(){
return this.name;
}
public int getAge(){
return this.age;
}
public void say(){
System.out.println(this.getContent());
}
public abstract String getContent();
}
class Worker extends Person{
private float salary;
public Worker(String name,int age,float salary){
super(name,age);
this.setSalary(salary);
}
public void setSalary(float salary){
this.salary=salary;
}
public float getSalary(){
return this.salary;
}
public String getContent(){
return "工人信息------>姓名:"+super.getName()+",年齡:"+super.getAge()+",工資:"+this.getSalary();
}
}
class Student extends Person{
private float score;
public Student(String name,int age,float score){
super(name,age);
this.setScore(score);
}
public void setScore(float score){
this.score=score;
}
public float getScore(){
return this.score;
}
public String getContent(){
return "學生信息------>姓名:"+super.getName()+", 年齡:"+super.getAge()+",成績:"+this.getScore();
}
}
public class OODemo11{
public static void main(String []args){
Person p=null;
p=new Student("張三",23,90);
p.say();
p=new Worker("李師傅",26,3000);
p.say();

}
}

運行結果:

學生信息------>姓名:張三, 年齡:23,成績:90.0
工人信息------>姓名:李師傅,年齡:26,工資:3000.0

❻ java多態性的編程題目

publicclassPeople{
privateStringname;
privateIntegerage;
privateStringphone;
publicStringgetMessage(){
return"People:姓名:"+name+",年齡:"+age+",電話:"+phone;
}
publicStringgetName(){
returnname;
}
publicvoidsetName(Stringname){
this.name=name;
}
publicIntegergetAge(){
returnage;
}
publicvoidsetAge(Integerage){
this.age=age;
}
publicStringgetPhone(){
returnphone;
}
publicvoidsetPhone(Stringphone){
this.phone=phone;
}
}
{
privateIntegerid;

publicStringgetMessage(Integerid){
return"Student:姓名:"+super.getName()+",年齡:"+super.getAge()+",電話:"+super.getPhone()+",學號:"+id;
}

publicIntegergetId(){
returnid;
}

publicvoidsetId(Integerid){
this.id=id;
}

}
publicclassS{

publicstaticvoidmain(String[]args){
Studentstudent=newStudent();
student.setId(13);
student.setName("李三");
student.setAge(18);
student.setPhone("123-4567-8901");
System.out.println(student.getMessage(student.getId()));
}

}

❼ java 關於繼承、多態的練習題

菜鳥的嘗試,如果有什麼錯誤和不妥,歡迎指出。

Public class child{

}

Public class error1 extends child(
érror1(){
}

Void testFail(){

}

}

Public class error2 extends child(
érror2(){
}

Void steals(){

}

}

Public class parents{
parents(){

}

Public static void print(child error){
If(error instanceof error1){
Print("談心")
}

If(error instanceof error2){
Print("挨打")
}
}

Public static void main (string[] args){
Parents parent= new parents();

Error errora1 = new error1();

Error errora2 = new error2();

Parent.print(errora1);

Parent.print(errora2);

}

大概就這樣了,純手機敲出來的,很多語法不對,
但是看下思路還是看出來的。

❽ java習題,多態的用法,這段代碼無法輸出結果,誰能幫我找出錯誤!

你的 Test 類應該這樣寫:

publicclassTest{

publicstaticvoidmain(String[]args){
//Operationop=newOperation(10.0,20.0);
Operationo;
o=newOpeartionAdd(10.0,20.0);
o.getResult();
o=newOpeartionCha(10.0,20.0);
o.getResult();
}
}

你的OpeartionCha類中的getResult 方法如果是希望它是覆蓋父類的 getResult 方法的話,那麼OpeartionCha類中的getResult 方法和父類的 getResult 方法應該參數聲明是一致的,即OpeartionCha類中的getResult 方法 也應該是無參的才對。所以OpeartionCha類 應該是這樣:

{

publicOpeartionCha(){
}

publicOpeartionCha(doublenum1,doublenum2){
super(num1,num2);
}

@Override
publicvoidgetResult(){
doublecha=(super.getNum1()-super.getNum2());
System.out.println(cha);
}
}

建議寫代碼的時候如果是覆寫方法,把 @Override 加上,這樣如果覆寫有誤,編譯階段就可以給出提醒。

❾ Java關於繼承、多態(介面和包)的編程題

package animal.mammal;
// 狗類
public class Dog extends Mammal{
void speak(){
System.out.println("Woof!");
}
void yaoweiba(){
System.out.println("Tail wagging...");
}
void qitaoshiwu(){
System.out.println("begging for food...");
}
}
// 同理寫貓,馬,豬,其中都必須有方法 void speak() 輸出各不相同,其他方法自定義
public class Cat extends Mammal{
}
public class Horse extends Mammal{
}
public class Pig extends Mammal{
}
// 另外是寵物貓,寵物狗的
package animal.mammal.pet;
public class PetCat extends Cat{
doucle price = 40.00;
String ownername = peter;
void Price(){
System.out.println("PetCat price:" + this.price);
}
void Owner(){
System.out.println("this's " + this.ownername +" petCat");
}
}
// 同理寫寵物狗的,如果需要 get/set 則為 price,ownername 加上該方法
public class PetDog extends Dog{
}

❿ 一道關於Java多態的應用的習題 請各位編程大神幫忙

手打太繁瑣,這么簡單還求方法?而且telephoneCharge方法,直接
public double telephoneCharge(int type,double time)就可以了,還重載2個方法做什麼(變數就時間和計費方式而已,甚至計費方式都不需要,直接根據時間來,超過多少時間是第一種,超過後是第二種),當type=1是第一種,=2是第二種。money=money-(time-3);money=money-time*rate;
測試類還要寫???騷年慢慢等吧。。。

閱讀全文

與java多態習題相關的資料

熱點內容
卸載聯想app哪個好 瀏覽:719
php文字轉圖片 瀏覽:328
豆客後台怎麼加密碼 瀏覽:574
jpg轉換pdf破解版 瀏覽:978
php基礎書籍推薦 瀏覽:775
伺服器與外網不通如何驗證 瀏覽:351
電子版是不是就是文件夾 瀏覽:50
游戲屬性文件加密 瀏覽:462
如何讓安卓手機桌面圖標下移 瀏覽:528
ubuntuphp5環境搭建 瀏覽:99
賭癮解壓視頻 瀏覽:917
晉城移動dns伺服器地址 瀏覽:294
php開源文庫系統 瀏覽:134
android記事本源碼 瀏覽:407
安卓11小游戲怎麼玩法 瀏覽:190
gif有損壓縮 瀏覽:937
windows下安裝linux命令操作 瀏覽:844
米家app怎麼設置進門亮燈 瀏覽:652
任我行伺服器為什麼會影響截圖 瀏覽:296
安卓留言板怎麼刪除 瀏覽:18