1. java繼承多態的練習題

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

2. 求 JAVA傳參、繼承、多態、介面練習題

JAVA傳參、繼承、多態、介面其實這個東西沒有什麼練習題的。都是概念性回的東西、需要的是答你理解,做練習題不如去想想它們應該怎麼做。我當年學習的時候老師講的很生動,至今都沒有忘記。傳參:等於拷貝。意思就是說把要傳的東西拷貝進來。繼承:人是一個大類(父類)、男人、女人是人類的繼承(子類)。每個子類都有自己的方法也可以沒有,子類可以調用父類所有的方法。多態:還用人舉例。比如說人有一個構造函數是吃一碗飯,而在男人這個子類中重載一下父類的構造函數變成吃兩碗飯,女人重載一下父類的構造函數變成吃水果。這個就叫做多態介面:介面是抽象的,這個解釋比較逗人。你的fuqin欠了一比錢還不清了,難么需要你來還。如果你也還不清的話那麼需要你的下一代償還。也就是說介面定義一個方法但是它並沒有實現這個方法那麼引入介面的類就會自動的生成這個方法,讓你實現它。好了給你解釋完了,我也重新溫習了一下

3. 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);

}

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

4. JAVA多態經典例題

System.out.println("1--" + a1.show(b));
a1是A類引用指向A類對象,不存在多態,一定調用A類方法。類方法有兩個show(D)和show(A),b是B類引用無法轉換為D類引用,但可以轉換為A類引用,因此調用show(A),輸出A and A。
System.out.println("2--" + a1.show(c));
輸出A and A,原因同上。
System.out.println("3--" + a1.show(d));
調用show(D),輸出A and D。
System.out.println("4--" + a2.show(b));
a2是A類引用指向B類對象,可能存在多態。b是B類引用無法轉換為D類引用,但可以轉換為A類引用,因此調用show(A),而B類重寫了show(A),因此調用的是重寫後的show(A),輸出B and A。
System.out.println("5--" + a2.show(c));
同上,C類引用無法轉換為D類引用,但可以轉換為A類引用,因此調用show(A),輸出B and A。
System.out.println("6--" + a2.show(d));

調用show(D),show(D)又調用父類即A類的show(D),輸出A and D
System.out.println("7--" + b.show(b));
b是B類引用指向B類對象,不存在多態,一定調用B類方法。B類一共有三個方法:重寫自A類的show(A)和show(D),以及新定義的show(B)。show(b)調用show(B)方法,輸出B and B
System.out.println("8--" + b.show(c));
C類繼承自B類,也調用show(B)方法,輸出B and B
System.out.println("9--" + b.show(d));
調用show(D),show(D)又調用父類即A類的show(D),輸出A and D

5. java題目,求助,多態。

publicclass多態
{
publicstaticvoidmain(String[]args)
{
.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();
}
}
}

6. 求java關於面向對象與繼承多態習題·各種·最好是有代表性的·

你只看到它們的形 而沒理解它們在實際項目中的用途,所以學起來很苦悶

建議你去看看專 《大屬話設計模式》《設計模式之禪》兩本書,學設計模式是面向對象編程的一種快速入門的方法

理解設計模式後,你就知道繼承 介面 多態這些東西 要如何用 為什麼要這樣用了

7. Java類的多態練習

public class Exam
{
public static void main(String[] args)
{
(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;
}

8. java 多態練習的編程!!!

public class ShapeTextDrive {
//圖形隨機演算法
//隨機1,2,3.
//一圓,二方,三阿米巴。
public static Shape getShapeByRandom() {

double num = (int)(Math.random()*3+1); //取1或2或3的隨機數
if(num == 1) {
return new Circle();
}else if (num == 2){
return new Square();
}else if(num == 3) {
return new Ameba();
}
return null;
}

public static void main(String[] args) {
//隨機取一個實現shape介面的圖形實例
Shape shape = ShapeTextDrive.getShapeByRandom();

//傳入實例,輸出計算結果。
ShapeTextDrive.getCircumferenceAndArea(shape);

//傳入circle實例,輸出計算結果。
//ShapeTextDrive.getCircumferenceAndArea(new Circle());

}
public static void getCircumferenceAndArea(Shape shape) {
shape.getCircumference();
shape.getArea();
}

//不使用介面的新增方法。
public static void getCircumferenceAndArea(Circle circle) {
circle.getCircumference();
circle.getArea();
}
}

//定義計算周長和面積方法的介面
interface Shape {
abstract double getCircumference();
abstract double getArea();
}

class Circle implements Shape {
public double getCircumference() {
//TODO:
System.out.println("計算圓的周長");
return -1;
}

public double getArea() {
//TODO:
System.out.println("計算圓的面積");
return -1;
}
}

class Square implements Shape {
public double getCircumference() {
//TODO:
System.out.println("計算方的周長");
return -1;
}

public double getArea() {
//TODO:
System.out.println("計算方的面積");
return -1;
}
}

class Ameba implements Shape {
public double getCircumference() {
//TODO:
System.out.println("計算阿米巴形狀的周長");
return -1;
}

public double getArea() {
//TODO:
System.out.println("計算阿米巴形狀的面積");
return -1;
}
}

9. 一個關於java 的多態題目怎麼編程

你好!很高興為你解答。
借鑒了樓上,認為樓上講得不夠體現,雖然核心代碼編了出來了,但沒能講到多態的點子上。我稍作修改,樓主請看:
運行結果:
********************
汽車在行駛
摩托車在行駛
自行車在行駛
************代碼如下*************

package TestPolymorphism;

/**
*
* @author 葉科良,568145421
*/
public class TestPolymorphism {

public static void main(String[] args) {
TravelTool travelTool = new Car();
travelTool.run();//一樣的語句,不一樣的功能,改動處則少
travelTool = new Moto();
travelTool.run();//一樣的語句,不一樣的功能,改動處則少
travelTool = new Bike();
travelTool.run();//一樣的語句,不一樣的功能,改動處則少
}
}

abstract class TravelTool {

abstract void run() ; //行駛

abstract void stop(); //剎車
}
class Car extends TravelTool {

void run() {
System.out.println("汽車在行駛");
}

void stop() {
System.out.println("汽車在剎車");
}
}

class Moto extends TravelTool {

void run() {
System.out.println("摩托車在行駛");
}

void stop() {
System.out.println("摩托車在剎車");
}
}

class Bike extends TravelTool {

void run() {
System.out.println("自行車在行駛");
}

void stop() {
System.out.println("自行在剎車");
}
}

10. java多態性的編程題目

publicclassPeople{
privateStringname;
privateIntegerage;
privateStringphone;
publicStringgetMessage(){
"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()));
}

}