由于练习构造方法的代码都类似,就不一一贴出来了。
package structureMethodDemo;
public class Cat {
String colour; //颜色
char sex;//性别
String type;//品种
int age;//年龄
public Cat() {
super();
}
public Cat(String colour, char sex, String type, int age) {
super();
this.colour = colour;
this.sex = sex;
this.type = type;
this.age = age;
}
public void play() {
System.out.println(this.colour+this.type+"正在play...");
}
public void catchMouse() {
System.out.println(this.colour+this.type+"正在抓老鼠...");
}
@Override
public String toString() {
return "Cat [colour=" + colour + ", sex=" + sex + ", type=" + type + ", age=" + age + "]";
}
}
package structureMethodDemo;
public class CatTest {
public static void main(String[] args) {
Cat c = new Cat();
c.colour = "橘色";
c.sex = '母';
c.type = "加菲";
c.age = 3;
System.out.println(c.toString());
c.play();
Cat b = new Cat("黑色", '公', "田园猫", 2);
System.out.println(b.toString());
b.catchMouse();
}
}
执行结果:
Cat [colour=橘色, sex=母, type=加菲, age=3]
橘色加菲正在play...
Cat [colour=黑色, sex=公, type=田园猫, age=2]
黑色田园猫正在抓老鼠...
package structureMethodDemo;
public class Cup {
String colour; //颜色
String texture; //质地
double height; //高度
int volume;//容积
String capColour;//盖子的颜色
public Cup() {
super();
}
public Cup(String colour, String texture, double height, int volume) {
super();
this.colour = colour;
this.texture = texture;
this.height = height;
this.volume = volume;
}
public Cup(String colour, String texture, double height, int volume, String capColour) {
super();
this.colour = colour;
this.texture = texture;
this.height = height;
this.volume = volume;
this.capColour = capColour;
}
@Override
public String toString() {
if (capColour == null) { //这样感觉有问题
return "Cup [colour=" + colour + ", texture=" + texture + ", height=" + height + ", volume=" + volume + "]";
} else {
return "Cup [colour=" + colour + ", texture=" + texture + ", height=" + height + ", volume=" + volume + ", capColour=" + capColour + "]";
}
}
public void water() {
System.out.println(this.colour+this.texture+"正在接水...");
}
public void broken() {
if (capColour == null) { //同理
System.out.println(this.colour+this.texture+"破碎了.....");
} else {
System.out.println("带盖子的"+this.colour+this.texture+"破碎了.....");
}
}
}
package structureMethodDemo;
public class CupTest {
public static void main(String[] args) {
Cup c = new Cup();
c.colour = "灰色";
c.texture = "塑料杯";
c.height = 14.05;
c.volume = 550;
System.out.println(c.toString());
c.water();
Cup b = new Cup("透明", "玻璃杯", 12.55, 300);
System.out.println(b.toString());
b.broken();
Cup d = new Cup("黑色", "塑料杯", 13.55, 350, "白色");
System.out.println(d.toString());
d.broken();
}
}
执行结果:
Cup [colour=灰色, texture=塑料杯, height=14.05, volume=550]
灰色塑料杯正在接水...
Cup [colour=透明, texture=玻璃杯, height=12.55, volume=300]
透明玻璃杯破碎了.....
Cup [colour=黑色, texture=塑料杯, height=13.55, volume=350, capColour=白色]
带盖子的黑色塑料杯破碎了.....
总结:
1.怎么去理解我们写的类时一个数据类型,我们new出来的对象是一个数据(int a=1)
Phone p=new Phone();//调用Phone()的构造方法
2.构造方法
被new关键字调用,在堆内存中开辟空间
系统会默认给我们一个无参的构造方法,但是如果你写了构造方法,那么默认的构造方法就会失效
无参的构造方法:new Phone();这个构造方法构造出来的对象就是一个全是默认的对象,需要接下来自己赋值
有参的构造方法:new phone("金色","华为");和this关键字搭配使用,省略之前自己赋值的那个过程
3.如果写了构造方法,那么系统则不会提供该方法
4.this关键字
指代调用当前方法的那个对象
书写过程总结:
1.思考该类的静态属性,在类中设计好变量,
(考虑颗粒度,不需要写的太细,也不要写的太普遍)
2.为了后期测试方便,给两个构造方法,一个有参,一个无参,构造方法的重载
可以自动生成,alt+shift+s——>Generate...using Fields...,
选择对应的属性,来生成两个构造方法
(super()可删可不删,不影响使用)
3.设计两到三个行为
4.自动生成一个类似于自我介绍的方法,方便后面查看数据
alt+shift+s——>Generate toString...
注意:
1.一个源文件(.java)中可以包含多个类
(具体写代码的过程,对大家要求一个.java文件中只能有一个class)
代码>规范>约定
2.但是只能有有一个类前面能加public,并且被public所修饰的类名必须和源文件名相同
3.类是java的执行单位,无论一个源文件中有多少个class,
编译之后的.class会和源文件中的class一一对应
之后有再补充。