构造方法

来源:互联网 发布:g76内螺纹编程实例 编辑:程序博客网 时间:2024/06/06 05:04
/**
 * @author Rory
 * @category 创建三个构造方法    执行不同的操作 在main方法中测试函数的调用
 */
public class Gouzao {
int a,b;
double c,d,e;
String f,g;

//传递两个整数值并找出其中最大的那个
public Gouzao(int a,int b){
this.a=a;
this.b=b;
if(a>b){
System.out.println("最大值是"+a);
}else{
System.out.println("最大值是"+b);
}
}

//传递三个double值并求出其乘积
public Gouzao(double c,double d,double e){
this.c=c;
this.d=d;
this.e=e;
double k=c*d*e;
System.out.println("三者的乘积是"+k);
}



//传递两个字符串并检查是否相同
public Gouzao(String f, String g) {
super();
this.f = f;
this.g = g;
if(f==g){
System.out.println("这两个字符串是相同的");
}else{
System.out.println("他们是不相同的");
}

}

}

/**
 * @author Rory
 * @category 构造方法测试
 */
public class GouzaoTest {


public static void main(String[] args) {
Gouzao gouzao1=new Gouzao(23, 21);

Gouzao gouzao2=new Gouzao(1.1, 2.0, 3.1);

Gouzao gouzao3=new Gouzao("部同", "部同");

}


}

0 0
原创粉丝点击