Head First Java_Study Notes_Chapter 2 A Trip to Objectville

Chapter 2 A Trip to Objectville

1.Thinking about Objects
Things an object knows about itself are instance variables.
Things an object can do are methods.

2.Making objects
Use two classes to create and use an object. One for the type of object you want to use, and another class to test the new class.

1)Write the class:
public class Dog 
{
	int size;
	String breed;
	String name;
	
	void bark()
	{
		System.out.println("Ruff");
	}
}

2)Write the class test drive:
public class DogTestDrive 
{
	public static void main(String[] args)
	{
		Dog d = new Dog();
		d.size = 40;
		d.bark();
	}
	
}

3.Get out of main
The two uses of main: to test the real class and to launch/start the java application

Exercise : 
A.We should new a TapeDeck t before we give it values.
class TapeDeck {
	boolean canRecord = false;
	
	void playTape() {
		System.out.println("tape playing");
	}
	
	void recordTape() {
		System.out.println("tape recording");
	}
}

class TapeDeckTestDrive {
	public static void main (String[] args){
		
		TapeDeck t = new TapeDeck();
		t.canRecord = true ;
		t.playTape();
		
		if (t.canRecord == true) {
			t.recordTape();
		}
	}
}

B.the example didn't define a playDVD method.
class DVDplayer {
	boolean canRecord = false;
	
	void playDVD() {
		System.out.println("DVD playing");
	}
	
	void recordDVD() {
		System.out.println("DVD recording");
	}
}

class TapeDeckTestDrive {
	public static void main (String[] args){
		
		DVDplayer d = new DVDplayer();
		d.canRecord = true ;
		d.playDVD();
		
		if (d.canRecord == true) {
			d.recordDVD();
		}
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值