1、寫一個Student類,定義姓名(name)、年齡(age)、性別(sex) 等屬性,還有學習(study)方法,並提供有參和無參兩個構造 函數。另寫一個Test類定義主函數,創建三個Student類型 對象並給每個對象的屬性進行賦值,調用study方法。
2、寫一個Dog類,定義顏色(color)、年齡(age)等屬性,還有睡 覺(sleep)方法,並提供有參和無參兩個構造 函數。另寫一個 Test類定義主函數,創建三個Dog類型對象並給每個對象的 屬性進行賦值,調用sleep方法。
3、寫一個Teacher類,定義姓名(name)、年齡(age)、工號(number) 等屬性,還有吃飯(eat)方法,並提供有參和無參兩個構造 函 數。另寫一個Test類定義主函數,創建三個Teacher類型 對 象並給每個對象的屬性進行賦值,調用eat方法。
4、寫一個汽車(Car)類,定義顏色(color)、名字(name),等屬性還 有跑(run)的方法,並提供有參和無參兩個構造函數。另寫一 個Test類定義主函數,創建三個Car類型 對 象並給每個對 象的屬性進行賦值,調用run方法。
5、寫一個貓(Cat)類,定義顏色(color)、名字(name)、體重(weight) 等屬性,還有玩耍(play)的方法,並提供有參和無參兩個構造 函數。另寫一個Test類定義主函數,創建三個Cat類型對 象 並給每個對象的屬性進行賦值,調用play方法。
package day12_oo; /* * 定義各種類 * 構造函數:1.構造函數必須和類名一樣(包括大小寫) 2.構造函數不允許手工調用 ,在對象的構造過程中自動調用一次 * 3.構造函數沒有返回值類型(意思連void都沒有) * 如果一個類沒有定義任何構造方法,系統會默認添加一個公開的無參構造方法 */ public class Test { public static void main(String[] args){ Student alex = new Student("Alex",23,true); Student jay = new Student("Jay",23,true); Student moses = new Student("Moses",23,true); alex.study("Alex"); Dog pointpoint = new Dog("waite",2); Dog smallyellow = new Dog("yellow",1); Dog smallduck = new Dog("orange",2); smallyellow.sleep("smallyellow"); Teacher zhang = new Teacher("zhang",25,"201610812188"); Teacher wang = new Teacher("wang",22,"201610812105"); Teacher wu = new Teacher("wu",18,"201610812166"); wu.eat("wu"); Car bmw = new Car("waite","bmw"); Car byd = new Car("red","byd"); Car wlrg = new Car("green","wlrg"); wlrg.run("wlrg"); Cat mimi = new Cat("waite","mimi",15.0); Cat miaomiao = new Cat("black","miaomiao",8.0); Cat xixi = new Cat("blue","xixi",10.0); mimi.play("mimi"); } } //定義一個學生類還有學習方法 class Student{ String name; int age; boolean sex; public Student(){}//無參構造方法 public Student(String name,int age,boolean sex){}//有參構造方法 void study(String name){ System.out.println(name+"正在學習"); } } //定義一個Dog類還有睡覺方法 class Dog{ String color; int age; public Dog(){}//無參構造方法 public Dog(String color,int age){}//有參構造方法 void sleep(String color){ System.out.println(color+"正在睡覺"); } } //寫一個Teacher類,定義姓名(name)、 //年齡(age)、工號(number) 等屬性,還有吃飯(eat)方法。 class Teacher{ String name; int age; String number; public Teacher(){}//無參構造方法 public Teacher(String name,int age,String number){}//有參構造方法 void eat(String name){ System.out.println(name+"老師在吃飯"); } } //寫一個汽車(Car)類,定義顏色(color)、名字(name),等屬性還 //4、有跑(run)的方法。 class Car{ String color; String name; public Car(){} public Car(String color,String name){} void run(String name){ System.out.println(name+"車在跑"); } } //寫一個貓(Cat)類,定義顏色(color)、名字(name)、體重(weight) //5、 等屬性,還有玩耍(play)的方法。 class Cat{ String color; String name; Double weight; public Cat(){}//無參構造方法 public Cat(String color,String name,double weigth){}//有參構造方法 void play(String name){ System.out.println(name+"小貓正在玩"); } }
