资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

java继承关系的代码 java程序设计实例

JAVA 继承的代码 求帮助 在线等

简单写了下,希望对你有帮助

成都创新互联主要业务有网站营销策划、成都网站制作、成都网站设计、外贸营销网站建设、微信公众号开发、微信小程序H5建站、程序开发等业务。一次合作终身朋友,是我们奉行的宗旨;我们不仅仅把客户当客户,还把客户视为我们的合作伙伴,在开展业务的过程中,公司还积累了丰富的行业经验、营销型网站资源和合作伙伴关系资源,并逐渐建立起规范的客户服务和保障体系。 

建议你找一下Java转型方面的资料

class Vehicle{

protected void vehicleRun(){

System.out.println("vehicle!!!");

}

}

class Truck extends Vehicle{

//子类覆写父类“vehicleRun方法”

protected void vehicleRun(){

System.out.println("It's Truck!!!");

}

protected void truckRun(){

System.out.println("truck!!!");

}

}

class Test{

public static void main(String [] args){

Vehicle v = new Vehicle();

v.vehicleRun(); //父类方法

//向上转型

Vehicle v1 = new Truck();

v1.vehicleRun(); //子类覆写后方法

//向下转型

Truck t = (Truck)v1;

t.vehicleRun();

t.truckRun();

}

}

JAVA关于继承的程序

class Student extends Person{

private String studentID;

public Student(){

super();

}

public String getStudentID() {

return studentID;

}

public void setStudentID(String studentID) {

this.studentID = studentID;

}

}

class Person{

protected String name;

protected int age;

protected String phonenumber;

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getPhonenumber() {

return phonenumber;

}

public void setPhonenumber(String phonenumber) {

this.phonenumber = phonenumber;

}

}

写一个java程序,主要是要运用数组,还要体现父类子类间的继承关系。

父类:

public class Bird{

public void sound(){System.out.println("I'm a bird!");}

public void fly(){System.out.println("I can fly!");}

public void body(){System.out.println("I have two wings and two legs!");}

}

麻雀:

public class Sparrow extends Bird{

int age;

int weight;

public void sound(){System.out.println(I'm a sparrow!);}

public void printAge(){System.out.println("My age is "+age);}

public void printWeight(){System.out.println("My weight is "+weight);}

public void setAge(int age){this.age=age;}

public void setWeight(int wight){this.weight=weight;}

}

鸵鸟:

public class Ostrich extends Bird{

int speed;

int height;

public void sound(){System.out.println(I'm a ostrich!);}

public void fly(){Syttem.out.println("I can't fly!");}

public void printSpeed(){System.out.println("My speed is "+speed);}

public void printHeight(){System.out.println("My height is "+weight);}

public void setSpeed(int speed){this.speed=speed;}

public void setHeight(int hight){this.height=height;}

}

鹰 就留给LZ自己练习了~~

至于数组的话,我认为是在测试的时候用的,如下:

public static void main(String[] arg0){

Bird[] birds=new Bird[10];

for(int i=0;i10;i++){

//这里随机生成对象,对于用户来说不知道birds数组中到底是什么对象

int temp = new java.util.Random().nextInt(4);

switch(temp){

case 0: birds[i]=new Bird();break;

case 1: birds[i]=new Sparrow();break;

case 2: birds[i]=new Ostrich();break;

//case 3: 这里楼主自己生成鹰的对象

}

}

//生成对象以后,我们如何得知数组中到底是什么动物呢?

//这就需要用到继承的特性了

//下面就来打印看看,我们到底生成了什么东西

for(Bird bird : birds){

//这里根据动物的叫声,就可以得知他到底是什么动物了,当然我们在生成的时候也将父类也生成了,所以也会有鸟的叫声。

bird.sound();

//如果要体现动物间不同的特性就这样,并且以此类推

if(bird instanceof Sparrow){((Sparrow)bird).printAge();}

if(bird instanceof Ostrich){((Ostrich)bird).printSpeed();}

//...

}

}

整个程序体现了子类与父类的多态性,声明的时候用父类型声明,生成对象的时候new的是具体的动物,那么在调用方法的时候,会根据具体声明的对象去调用相应的方法,也就是上面的sound方法,调用之后显然是各自的叫声。


名称栏目:java继承关系的代码 java程序设计实例
文章地址:http://cdkjz.cn/article/ddcjegs.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220