第一題:概念辨析
-
一個類與它的對象之間是什么關(guān)系?
類是對象的模板,對象是類的實體 -
如何定義一個類,由哪些部分組成,各個部分的作用是什么?
三部分: 成員變量:描述事物的狀態(tài)信息 構(gòu)造方法:創(chuàng)建事物對象 成員方法:描述事物能做什么 -
如何創(chuàng)建一個對象,如何使用該對象?
// 創(chuàng)建對象格式: 數(shù)據(jù)類型 變量名 = new 數(shù)據(jù)類型(); // 對象使用方式: 變量名.成員變量 變量名.成員方法 -
局部變量和成員變量的區(qū)別?
在類中的位置不同 成員變量:類中,方法外 局部變量:方法中或者方法聲明上(形式參數(shù)) 作用范圍不一樣 成員變量:類中 局部變量:方法中 初始化值的不同 成員變量:有默認值 局部變量:沒有默認值。 在內(nèi)存中的位置不同 成員變量:堆內(nèi)存 局部變量:棧內(nèi)存 生命周期不同 成員變量:隨著對象的創(chuàng)建而存在,隨著對象的消失而消失 局部變量:隨著方法的調(diào)用而存在,隨著方法的調(diào)用完畢而消失 -
構(gòu)造方法和成員方法的區(qū)別?
作用不同: 構(gòu)造方法:創(chuàng)建對象 成員方法:執(zhí)行某具體功能 命名不同: 構(gòu)造方法:類名一致 成員方法:自定義 返回值類型不同: 構(gòu)造方法:無返回值類型 成員方法:void或者確定的數(shù)據(jù)類型 調(diào)用不同: 構(gòu)造方法:new 關(guān)鍵字調(diào)用 成員方法:對象.成員方法名調(diào)用
第二題:語法練習(xí)
- 定義一個圓形Circle類。
- 屬性:
- r:半徑
- 構(gòu)造方法:
- 無參構(gòu)造方法
- 滿參構(gòu)造方法
- 成員方法:
- get/set方法
- showArea方法:打印圓形面積
- showPerimeter方法:打印圓形周長
- 屬性:
- 定義測試類,創(chuàng)建Circle對象,并測試。
- 代碼實現(xiàn),效果如圖所示:
-
開發(fā)提示:
面向周長公式:2 * 3.14* 半徑
圓形面積公式:3.14* 半徑^2
-
參考答案:
- Circle類:
public class Circle { int r; public Circle() { } public Circle(int r) { this.r = r; } public int getR() { return r; } public void setR(int r) { this.r = r; } public void showArea(){ System.out.println("半徑為:"+ r +",面積為:"+ ( 3.14 * r * r)); } public void showPerimeter(){ System.out.println("半徑為:"+ r +",面積為:"+ ( 2 * 3.14 * r)); } }- 測試類:
public class Test2 { public static void main(String[] args) { Circle circle = new Circle(8); circle.showArea(); circle.showPerimeter(); } }
第三題:語法練習(xí)
- 定義一個日期MyDate類。
- 屬性:
- year:年
- month:月
- day:日
- 構(gòu)造方法:
- 滿參構(gòu)造方法
- 成員方法:
- get/set方法
- showDate方法:打印日期。
- isBi方法:判斷當(dāng)前日期是否是閏年
- 屬性:
- 定義測試類,創(chuàng)建MyDate對象,并測試。
- 代碼實現(xiàn),效果如圖所示:
-
開發(fā)提示:
- 閏年:
- 普通年(不能被100整除的年份)能被4整除的為閏年。(如2004年就是閏年,1999年不是閏年);
- 世紀年(能被100整除的年份)能被400整除的是閏年。(如2000年是閏年,1900年不是閏年);
- 閏年:
-
參考答案:
- MyDate類:
public class MyDate { int year; int month; int day; public MyDate(int year, int month, int day) { this.year = year; this.month = month; this.day = day; } public int getYear() { return year; } public void setYear(int year) { this.year = year; } public int getMonth() { return month; } public void setMonth(int month) { this.month = month; } public int getDay() { return day; } public void setDay(int day) { this.day = day; } public void showDate() { System.out.println("日期:" + year + "年" + month + "月" + day + "日"); } public void isBi() { if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) { System.out.println(year + "年是閏年"); } else { System.out.println(year + "年不是閏年"); } } }
-
測試類:
public class Test3 { public static void main(String[] args) { MyDate date = new MyDate(1900, 1, 1); date.showDate(); date.isBi(); } }
第四題:語法練習(xí)
- 定義一個撲克Card類。
- 屬性:
- 花色
- 點數(shù)
- 構(gòu)造方法:
- 滿參構(gòu)造方法
- 成員方法:
- showCard方法:打印牌面信息
- 屬性:
- 定義測試類,創(chuàng)建Card對象,調(diào)用showCard方法。
- 代碼實現(xiàn),效果如圖所示:
-
參考答案:
- Card類:
public class Card { private String ds; // 點數(shù) private String hs; // 花色 public Card(String ds, String hs) { this.ds = ds; this.hs = hs; } public void showCard() { System.out.println( ds + hs ); } }- 測試類:
public class Test5 { public static void main(String[] args) { Card card = new Card("黑桃", "A"); card.showCard(); } }
第五題:語法練習(xí)
- 定義兩個類,經(jīng)理類Manager,程序員類Coder
- Coder類:
- 屬性:姓名,工號,薪資
- 構(gòu)造方法:無參構(gòu)造方法,滿參構(gòu)造方法
- 成員方法:
- get/set方法
- intro方法:打印姓名,工號信息
- showSalary方法:打印薪資信息
- work方法:打印工作信息
- Manager類:
- 屬性:姓名,工號,薪資
- 經(jīng)理的薪資有兩部分組成:基本工資+獎金
- 構(gòu)造方法:無參構(gòu)造方法,滿參構(gòu)造方法
- 成員方法:
- get/set方法
- intro方法:打印姓名,工號信息
- showSalary方法:打印薪資信息
- work方法:打印工作信息
- 屬性:姓名,工號,薪資
- 定義測試類,創(chuàng)建Manager對象,創(chuàng)建Coder對象,并測試。
- 代碼實現(xiàn),效果如圖所示:
-
參考答案:
- Coder類:
package test4; public class Coder { private String name; private String id; private int salary; // 基本工資 public Coder() { } public Coder(String name, String id, int salary) { this.name = name; this.id = id; this.salary = salary; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getId() { return id; } public void setId(String id) { this.id = id; } public int getSalary() { return salary; } public void setSalary(int salary) { this.salary = salary; } public void showSalary() { System.out.println("基本工資為" + salary + ",獎金無"); } public void intro() { System.out.println("程序員姓名:" + name); System.out.println("工號:" + id); } public void work() { System.out.println("正在努力寫代碼......"); } }- Manager類:
public class Manager { private String name; private String id; private int[] salary; // 基本工資 + 獎金 public Manager() { } public Manager(String name, String id, int[] salary) { this.name = name; this.id = id; this.salary = salary; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getId() { return id; } public void setId(String id) { this.id = id; } public int[] getSalary() { return salary; } public void setSalary(int[] salary) { this.salary = salary; } public void showSalary() { System.out.println("基本工資為" + salary[0] + ",獎金為" + salary[1]); } public void intro() { System.out.println("經(jīng)理姓名:" + name); System.out.println("工號:" + id); } public void work() { System.out.println("正在努力的做著管理工作,分配任務(wù),檢查員工提交上來的代碼....."); } }- 測試類:
public class Test5 { public static void main(String[] args) { int[] salary = {15000, 3000}; Manager m = new Manager("James", "9527",salary ); m.intro(); m.showSalary(); m.work(); System.out.println("============"); Coder c = new Coder(); c.setName("Kobe"); c.setId("0025"); c.setSalary(10000); c.intro(); c.showSalary(); c.work(); } }
第六題:語法練習(xí)
-
老師類Teacher
- 屬性:姓名name,年齡age,講課內(nèi)容content
- 成員方法:吃飯eat方法,講課teach方法
-
學(xué)生類Student
- 屬性:姓名name,年齡age,學(xué)習(xí)內(nèi)容content
行為:吃飯eat方法, 學(xué)習(xí)study方法
- 屬性:姓名name,年齡age,學(xué)習(xí)內(nèi)容content
代碼實現(xiàn),效果如圖所示:
-
參考答案:
- Teacher類:
package test6; /* */ public class Teacher { private String name; private int age; private String content; /** * 講課方法 */ public void jiangke() { System.out.println("年齡為"+age+"的"+name+"正在亢奮的講著"+content+"的知識........"); } public void eat() { System.out.println("年齡為"+age+"的"+name+"正在吃飯...."); } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public Teacher() { } public Teacher(String name, int age, String content) { this.name = name; this.age = age; this.content = content; } }- 學(xué)生類:
public class Student { private String name; private int age; private String content; public void study() { System.out.println("年齡為"+age+"的"+name+"正在專心致志的聽著"+content+"的知識........"); } public void eat() { System.out.println("年齡為"+age+"的"+name+"正在吃飯...."); } public Student(String name, int age, String content) { this.name = name; this.age = age; this.content = content; } public Student() { } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } }- 測試類:
public class Test6 { public static void main(String[] args) { Teacher t = new Teacher(); t.setName("周老師"); t.setAge(30); t.setContent("java面向?qū)ο?); t.eat(); t.jiangke(); Student stu = new Student("韓同學(xué)",18,"java面向?qū)ο?); stu.eat(); stu.study(); } }




