※J.Y Chen 的個人部落格 ※

Just Follow Our Heart And We will shine!

61 瀏覽人次

【程式語言】JAVA的第七堂課 |物件導向程式設計 (練習題)

Published: in JAVA by .

ch8.java

public class ch8 {
		public static void main(String[] args) {
			Dog Mydog = new Dog();
			Mydog.set_age(5);
			Mydog.set_name("Lucky");
			//Mydog.show();
			System.out.println("My dog name is " + Mydog.getName() + ", and i am " + Mydog.getAge() + " years old!!");
			Mydog.ruff();
		}
	
}

dog.java

class Dog {
	//data member
	private int age;
	private String name;
	private String breed;
	private double weight; 
	
	//member functions
	void ruff() {
		System.out.println(name + ":ruff!ruff!ruff!");
	}
	void move() {
		System.out.println("Dog forward 1 step!");
	}
	void change_color(String color) {
		System.out.println("This dog changed to " + color);
	}
	void set_name (String n) {
		name = n;
	}
	
	void set_age(int a) {
		age = a;
	}
	String getName() {
		return name;
	}
	
	int getAge(){
		return age;
	}
	
	/* void show() {
		System.out.println("my dog name is " + name + ",and i am " + age + " years old");
	}
	*/
	
}

Result:

©2019 - 2024 Henry Chen