导航:首页 > 编程语言 > 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多态习题相关的资料

热点内容
安卓11小游戏怎么玩法 浏览:186
gif有损压缩 浏览:929
windows下安装linux命令操作 浏览:840
米家app怎么设置进门亮灯 浏览:650
任我行服务器为什么会影响截图 浏览:294
安卓留言板怎么删除 浏览:16
做大厂程序员有什么感受 浏览:239
php文件只读 浏览:773
红色警戒3命令修改器112 浏览:432
安卓税收和苹果税是什么意思 浏览:445
快速排序算法的时间复杂度分析 浏览:111
大龄程序员困境 浏览:269
手机号忘了怎么登录农行app 浏览:572
商品信息管理系统php 浏览:8
效果器app怎么无线连接 浏览:404
clinux线程锁 浏览:851
怎么看新手机安卓充电器是不是原装 浏览:294
32单片机f4点灯源码 浏览:223
车载安卓导航开发者选项怎么开启 浏览:694
学生程序员兼职 浏览:360