文档库 最新最全的文档下载
当前位置:文档库 › JAVA实验

JAVA实验

学号:3130101208 姓名:俞欣辰电话:188********

夏第6周实验面向对象技术(3)

实验目的:

熟练掌握面向对象技术

实验内容:

1.编写一个抽象类的应用程序,要创建子类对象,调用相关的属性,方法输出(具体内容:属性,方法请自定,如抽象类为shape,子类为circle,也可抽象类为animal,子类dog, 可参考教材改写)

2.编写一个接口的应用程序:先定义一个接口,再定义一个类实现接口中的方法. 要创建子类对象,调用相关的属性,方法输出(具体内容:属性,方法请自定,可参考教材改写)

3.编写一个程序,实现方法的重载.可参考教材,内容自定.

4.编写一个程序,实现方法的覆盖.可参考教材,内容自定.

5.完成10.77.30.33上的有关题目(不用在这里上交),

实验结果:

1.代码:

abstract class animal{

String food;

String voice;

String kind;

public animal(String food,String voice){

this.food=food;

this.voice=voice;

}

abstract void show_kind();

public void show_voice(){

System.out.println("This animal's voice is "+voice);

}

abstract void show_food();

}

class dog extends animal{

public dog(String food,String voice){

super(food,voice);

}

public void show_kind()

{

System.out.println("The animal's kind is dog");

}

public String show_voice(String voice){

return voice;

}

public void show_food(){

System.out.println("Dog eats "+food);

}

}

public class sha{

public static void main(String args[]){

dog d1=new dog("meat","bark");

d1.show_kind();

d1.show_food();

d1.show_voice();

}

}

结果:

The animal's kind is dog

Dog eats meat

This animal's voice is bark

2. 代码:

interface hobby{

abstract void show_kind();

abstract void getHobby();

}

public class guitar implements hobby{

String feature,reason;

int years;

public guitar(String x1,String x2,int x3){

this.feature=x1;

this.reason=x2;

this.years=x3;

}

public void show_kind(){

System.out.println("This person's hobby is guitar");

}

public void getHobby(){

System.out.println("Guitar is "+feature);

System.out.println("I like guitar because it "+reason);

System.out.println("I've been playing guitar for "+years);

}

}

class guitar1{

public static void main(String args[])

{

guitar g1=new guitar("cool","makes me happy",5);

g1.show_kind();

g1.getHobby();

}

}

结果:

This person's hobby is guitar

Guitar is cool

I like guitar because it makes me happy

I've been playing guitar for 5

3. 代码:

class shw {

public double happy(int a,int b){

return Math.sqrt(a*b+b*b);

}

public double happy(int a,int b,int c){

return Math.sqrt(a*c+b*c+c*c);

}

}

public class cho{

public static void main(String[] args){

shw c1,c2;

c1=new shw();

c2=new shw();

System.out.println("First kind of calculate="+c1.happy(1,3));

System.out.println("Second kind of calculate="+c2.happy(2,3,4));

}

}

结果:

First kind of calculate=3.4641016151377544

Second kind of calculate=6.0

4. 代码:class country{

int pop,fam,male;

country(int a, int b, int c){

pop=a;

fam=b;

male=c;

}

public double sample(int d){

return pop*0.001;

}

public double sample(int e,int f){

return fam*0.001+male*0.002;

}

}

class zhejiang extends country

{

zhejiang(int a, int b, int c){

super(a,b,c);

}

public double sample(int g){

return 0;

}

public double sample(int h,int i){

return 1;

}

}

public class gg {

public static void main(String[] args){

zhejiang one=new zhejiang(1400000000,700000000,800000000);

System.out.println(one.sample(one.pop));

System.out.println(one.sample(one.fam,one.male ));

}

}

结果:0.0

1.0

相关文档