输出对象属性,方法
public class Homework07 {
//编写一个main方法
public static void main(String[] args) {
Dog dog1 = new Dog("jack", "白色", 11);
dog1.show();
//==============================
//new Test()是匿名对象,没有栈的引用,匿名对象使用后,就不能使用
//new Test().count1() 创建好匿名对象后,就带哦用count1()
new Test().count1();
Test t1 = new Test();
t1.count2();
t1.count2();
}
}
设计一个Dog类,有名字、颜色和年龄属性,定义输出方法show()
显示其信息。并进行创建对象,进行测试、【提示this.属性】
class Dog {
String name;
String color;
int age;
public Dog(String name, String color, int age) {
this.name = name;
this.color = color;
this.age = age;
}
public void show() {
System.out.println("名字:" + name);
System.out.println("颜色:" + color);
System.out.println("年龄:" + age);
}
//==============================
class Test {
int count = 9;//属性
public void count1() { //Test类的成员方法
count = 10;//count是属性,改成10
System.out.println("count1=" + count);
}
public void count2() {//Test类的成员方法
System.out.println("count1=" + count++);//后++,先输出9,在++变为10(9-10-11)
}
}