领养流浪猫问题是学习面向对象中构造方法的入门石。
抛开问题,首先了解构造方法:
概念:给对象的数据等属性进行初始化;
它是不同与普通方法的,其两者区别(构造方法特征)在于:
(1)构造方法的名称与所在类的名称必须相同。例如Cat类的构造方法是
Cat(){
}
(2)构造方法禁止定义返回值类型,即不使用void作为返回值类型,例如
public Cat(){
}
Cat前如若加void:
public void Cat(){
}
则变为普通方法;
(3)构造方法不需要return语句。所有构造方法都用于返回所在类的对象,那么也就可以省略了。
另外(4)构造方法只能在对象实例化时调用;
(5)一个类中可以有多个构造方法。
构造方法类型:无参构造和有参构造;
无参:
class Cat(){
System.out.println("我叫无参构造方法。");
}
有参:
class Cat(String name){ /*括号里可以添加多个参数,用逗号分隔开*/
System.out.println("我叫有参构造方法。");
}
用的时候是要在实例化时传入参数;
Cat a = new Cat("小喵");
注意:当没有指定的构造方法时,系统会自动添加无参的构造方法;
当有指定的构造方法时,无论是有参还是无参,都不会自动添加无参的构造方法;
了解以上后,源码如下:
class Cat{
//定义猫类
String name;
String sex;
String colors;
double age;
String city;
//成员变量(属性)
public Cat(String name,String sex,String colors,double age,String city){
//构造方法
this.name = name;
this.sex = sex;
this.colors = colors;
this.age = age;
this.city = city;
//this.访问成员变量
}
public String getName(){
return name;
}
public String getSex(){
return sex;
}
public String getColors(){
return colors;
}
public double getAge(){
return age;
}
public String getCity(){
return city;
}
public void showInfo(){
System.out.println("名称:" + name + "\t性别:" + sex + "\t花色:" + colors + "\t年龄:" + age + "\t城市:" + city);
//输出流浪猫信息
}
}
//定义领养者类
class Adopter{
String name;
String city;
//成员变量(属性)
public Adopter(String name,String city){
//构造方法
this.name = name;
this.city = city;
}
public String getName(){
return name;
}
public String getCity(){
return city;
}
public void showInfo(){
System.out.println("名字:" + name + "\t城市:" + city);
}
}
//定义测试类
public class TestCatAdopt {
public static void main(String[] args) {
Adopter p1 = new Adopter("李小明", "北京");
Adopter p2 = new Adopter("王小红", "上海");
Cat c1 = new Cat("小喵", "女", "白色", 1.1, "北京");
Cat c2 = new Cat("大壮", "男", "黑色", 1.2, "武汉");
Cat c3 = new Cat("小咪", "女", "黄色", 0.8, "广州");
System.out.println("领养者信息");
p1.showInfo();
p2.showInfo();
System.out.println("流浪猫信息");
c1.showInfo();
c2.showInfo();
c3.showInfo();
//信息
if (p1.getCity().equals(c1.getCity())) {
System.out.println(p1.getName() + "领养了" + c1.getName());
} else {
System.out.println(p1.getName() + "和" + c1.getName()+"不同城,不能领养。");
}
if (p2.getCity().equals(c2.getCity())) {
System.out.println(p2.getName() + "领养了" + c2.getName());
} else {
System.out.println(p2.getName() + "和" + c2.getName()+"不同城,不能领养。");
}
}
}
程序运行结果如下:
另外如若对于this关键字运用熟练,可以用this调用和调换成员属性及输出顺序;
源码如下:
//定义流浪猫类
class Cat{
//成员变量(属性)
String name;
String color;
String city;
//成员方法(行为)
// 构造方法
public Cat(){
}
public Cat(String name){
this();
this.name = name;
}
public Cat(String name, String color){
this(name);
this.color = color;
}
public Cat(String name, String color, String city){
//this表示当前对象
//this.访问成员变量
this(name, color);
this.city = city;
}
public String getName(){
return name;
}
public String getCity(){
return city;
}
public void showInfo(){
System.out.println("姓名:" + name + "\t颜色:" + color + "\t所在城市:" + city);
}
}
//定义领养者类
class Adopter{
//成员变量(属性)
String name;
String city;
//成员方法(行为)
//构造方法
public Adopter(){
}
public Adopter(String n){
name = n;
city = "北京";
}
public Adopter(String n, String ct){
name = n;
city = ct;
}
public String getName(){
return name;
}
public String getCity(){
return city;
}
public void showInfo(){
System.out.println("姓名:" + name + "\t所在城市:" + city);
}
}
public class TestCatAdopter{
public static void main(String[] args){
//创建两个领养者对象
Adopter a1 = new Adopter("小龙");
Adopter a2 = new Adopter("小彩", "北京");
//创建3个流浪猫对象
Cat c1 = new Cat("小咪");
Cat c2 = new Cat("大壮 ", "黑色");
Cat c3 = new Cat("小喵", "白色", "北京");
System.out.println("查询到以下领养者的信息:");
a1.showInfo();
a2.showInfo();
System.out.println("查询到以下流浪猫的信息:");
c1.showInfo();
c2.showInfo();
c3.showInfo();
//领养
if(a1.getCity().equals(c2.getCity())){
System.out.println(a1.getName() + "领养了同城的 " + c2.getName());
}else {
System.out.println(a1.getName() + "和 " + c2.getName() + "不在同一个城市,不能领养!");
}
if(a2.getCity().equals(c3.getCity())){
System.out.println(a2.getName() + "领养了同城的 " + c3.getName());
}else {
System.out.println(a2.getName() + "和 " + c3.getName() + "不在同一个城市,不能领养!");
}
}
}
运行结果如下:
第二种方法中利用了构造方法的重载,同一个类中可以定义多个构造方法,可以使用方法的重载,它是指在同一个类中,一个方法允许存在一个或一个以上的同名方法,只要它们的参数列表不同即可。参数列表包括参数的类型、参数的个数以及参数的顺序,三者中有一个不同即可。