【上机作业】【java】2010.3.31 类的多态性
import java.text.DecimalFormat;
class math{
public int plus(int a,int b){
return a+b;
}
public String plus(double a,double b){
DecimalFormat format=new DecimalFormat(“#.000”);
double oldDouble;
oldDouble=a+b;
String newDouble = format.format(oldDouble );
return newDouble;
}
public int minus(int a,int b){
return a-b;
}
public String minus(double a,double b){
DecimalFormat format=new DecimalFormat(“#.000”);
double oldDouble;
oldDouble=a-b;
String newDouble = format.format(oldDouble );
return newDouble;
}
public int multiply(int a,int b){
return a*b;
}
public String multiply(double a,double b){
DecimalFormat format=new DecimalFormat(“#.000”);
double oldDouble;
oldDouble=a*b;
String newDouble = format.format(oldDouble );
return newDouble;
}
public int division(int a,int b){
if(b==0){
System.out.println(“error!”);
return 0;
}
else
return a/b;
}
public String division(double a,double b){
if(b==0){
System.out.println(“error!”);
return “0”;
}
else{
DecimalFormat format=new DecimalFormat(“#0.000”);
double oldDouble;
oldDouble=a/b;
String newDouble = format.format(oldDouble );
return newDouble;
}
}
public static void main(String []args){
math m=new math();
System.out.println(“1+2=”+m.plus(1,2));
System.out.println(“1-2=”+m.minus(1,2));
System.out.println(“1*2=”+m.multiply(1,2));
System.out.println(“1/2=”+m.division(1,2));
System.out.println(“——————————————–“);
System.out.println(“1.1+2.2=”+m.plus(1.1,2.2));
System.out.println(“1.1-2.2=”+m.minus(1.1,2.2));
System.out.println(“1.1*2.2=”+m.multiply(1.1,2.2));
System.out.println(“1.1/2.2=”+m.division(1.1,2.2));
}
}
class Animal{
String name,color;
double weight;
public Animal(String n,String c,double w){
name=n;
color=c;
weight=w;
}
void speak(){
System.out.println(“I’m “+name);
}
void eat(){
}
}
class Dog extends Animal{
public Dog(String n,String c,double w){
super(n,c,w);
}
void speak(){
System.out.println(“wang wang”);
}
void eat(){
System.out.println(“eat bone”);
}
}
class Rabbit extends Animal{
public Rabbit(String n,String c,double w){
super(n,c,w);
}
void speak(){
System.out.println(“aaaaaaaaaa”);
}
void eat(){
System.out.println(“eat grass”);
}
}
class display{
public static void main(String []args){
Dog d=new Dog(“Dog”,”yellow”,10.2);
Rabbit r=new Rabbit(“Rabbit”,”black”,5.5);
d.speak();
d.eat();
r.speak();
r.eat();
}
}
class Animal{
String name,color;
double weight;
public Animal(String n,String c,double w){
name=n;
color=c;
weight=w;
}
void speak(){
System.out.println(“I’m “+name);
}
void eat(){
}
}
class Dog extends Animal{
public Dog(String n,String c,double w){
super(n,c,w);
}
void speak(){
System.out.println(“wang wang”);
}
void eat(){
System.out.println(“eat bone”);
}
}
class Rabbit extends Animal{
public Rabbit(String n,String c,double w){
super(n,c,w);
}
void speak(){
System.out.println(“aaaaaaaaaa”);
}
void eat(){
System.out.println(“eat grass”);
}
}
class display{
public static void main(String []args){
Dog d=new Dog(“Dog”,”yellow”,10.2);
Rabbit r=new Rabbit(“Rabbit”,”black”,5.5);
d.speak();
d.eat();
r.speak();
r.eat();
}