java Homework07

输出对象属性,方法

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)
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值