构造方法与重载 创建一个类 为该类定义三个构造方法 分别执行下列操作 :1、传递两个整数值并找出其中较大的一个值 2、传递三个 double 值并求出其乘积 3、传递两个字符串值并检查其是否相同 4、在 main 方法中测试构造函数的调用
源代码:
public static void main(String[] args) {
t9 n1 = new t9(1,2);
t9 n2 = new t9(1.0,2.5,2.0);
t9 n3 =new t9("!", "*");
}
int a,b;
public t9(int a,int b) {
this.a=a;
this.b=b;
if(a>b) {
System.out.println("较大值为"+a);
}else
System.out.println("较大值为"+b);
}
double c,d,e,chengji;
public t9(double c,double d,double e) {
this.c=c;
this.d=d;
this.e=e;
chengji=c*d*e;
System.out.println("乘积为"+chengji);
}
String f,g;
public t9(String f,String g) {
this.f=f;
this.g=g;
if(f==g) {
System.out.println("两个字符串值相同");
}else
System.out.println("两个字符串值不相同");
}
结果示例:
 吾独矣 终极愿望世界和平
|