標籤:顯示 公司 輸出 show 建立 bre sys password with
(一)學習總結
1.閱讀下面程式,分析是否能編譯通過?如果不能,說明原因。應該如何修改?程式的運行結果是什嗎?為什麼子類的構造方法在運行之前,必須調用父 類的構造方法?能不能反過來?
class Grandparent { public Grandparent() { System.out.println("GrandParent Created."); } public Grandparent(String string) { System.out.println("GrandParent Created.String:" + string); }}class Parent extends Grandparent { public Parent() { System.out.println("Parent Created"); super("Hello.Grandparent."); }}class Child extends Parent { public Child() { System.out.println("Child Created"); }}public class Test{ public static void main(String args[]) { Child c = new Child(); }}
運行結果:
應該將Parent類中super("Hello.Grandparent.");放在該構造方法的第一句,放在輸出之前
修改後運行結果
只能子類調用父類的成員變數和成員方法,實現繼承,不能反過來
2.閱讀下面程式,剖析器中存在哪些錯誤,說明原因,應如何改正?正確程式的運行結果是什嗎?
···
class Animal{
void shout(){
System.out.println("動物叫!");
}
}
class Dog extends Animal{
public void shout(){
System.out.println("汪汪......!");
}
public void sleep() {
System.out.println("狗狗睡覺......");
}
}
public class Test{
public static void main(String args[]) {
Animal animal = new Dog();
animal.shout();
animal.sleep();
Dog dog = animal;
dog.sleep();
Animal animal2 = new Animal();
dog = (Dog)animal2;
dog.shout();
}
}
···
運行結果
第一行錯誤的原因是上轉型,只能調用子類繼承或者覆寫的方法,其中沒有sleep方法。 將其去掉
第二行錯誤的原因是下轉型需要加上“(類型)“, 改正:Dog dog =(Dog) animal;
運行結果
3.運行下列程式
···
class Person {
private String name ;
private int age ;
public Person(String name,int age){
this.name = name ;
this.age = age ;
}
}
public class Test{
public static void main(String args[]){
Person per = new Person("張三",20) ;
System.out.println(per);
System.out.println(per.toString()) ;
}
}
···
(1)程式的運行結果如下,說明什麼問題?
···
[email protected]
[email protected]
···
輸出類對象會預設調用object類的tostring方法
(2)那麼,程式的運行結果到底是什麼呢?利用eclipse開啟println(per)方法的源碼,查看該方法中又調用了哪些方法,能否解釋本例的運行結果?
出來了這個。。
(3)在Person類中增加如下方法
···
public String toString(){
return "姓名:" + this.name + ",年齡:" + this.age ;
}
···
重新運行程式,程式的執行結果是什嗎?說明什麼問題?
可參考教材P229
運行結果
覆寫了toString方法,調用的為覆寫後的方法,列印類名也預設調用覆寫後的方法
4.汽車租賃公司,出租汽車種類有客車、貨車和皮卡三種,每輛汽車除了具有編號、名稱、租金三個基本屬性之外,客車有載客量,貨車有載貨量,皮卡則同時具有載客量和載貨量。用物件導向編程思想分析上述問題,將其表示成合適的類、抽象類別或介面,說明設計思路。現在要建立一個可租車列表,應當如何建立?
建立思路:
將編號、名稱、租金定義為介面1,載客量為一個介面2,載貨量為一個介面3
客車類串連介面1和2,貨車串連介面1和3,皮卡串連介面1,2,3
5.閱讀下面程式,分析代碼是否能編譯通過,如果不能,說明原因,並進行改正。如果能,列出運行結果
···
interface Animal{
void breathe();
void run();
void eat();
}
class Dog implements Animal{
public void breathe(){
System.out.println("I‘m breathing");
}
void eat(){
System.out.println("I‘m eating");
}
}
public class Test{
public static void main(String[] args){
Dog dog = new Dog();
dog.breathe();
dog.eat();
}
}
···
修改後
···
package 作業三;
interface Animal{
void breathe();
void run();
void eat();
}
class Dog implements Animal{
public void breathe(){
System.out.println("I‘m breathing");
}
public void run() {
System.out.println("I‘m running");
}
public void eat(){
System.out.println("I‘m eating");
}
}
public class test{
public static void main(String[] args){
Dog dog = new Dog();
dog.breathe();
dog.eat();
}
}
···
運行結果
6.其他需要總結的內容。
實驗四
銀行類
···package test;
public class Bank {
static String bankName;//銀行名稱
private String name;
private String password;
private double balance;//賬戶餘額
private double turnover;//交易額
static void welcome(){ System.out.println("歡迎來到建設銀行");}//開戶public Bank(String name,String password,double turnover){ this.name = name; this.password= password; this.turnover = turnover; this.balance = turnover - 10; System.out.println(name+"開戶成功"+"餘額為:"+balance);}//存款方法public void depoist(double turnover){ balance = balance+turnover; System.out.println(name+"您的賬戶已存入"+balance+"元"+"當前餘額為:"+balance+"元");}//取款方法public void withdrawal(String password,double turnover){ if(this.password!=password) { System.out.println("密碼錯誤,重新輸入"); return; } if(balance-turnover>=0) { System.out.println("您已取出"+turnover+"元"+"當前餘額為"+(balance-turnover)); } else{ System.out.println("餘額不足"); } }static void welcomenext(){ System.out.println("請恁鞋帶好隨身物品,歡迎下次再來");}
}
public class Trade{
public static void main(String[] args){
Bank.bankName="建設銀行";
Bank.welcome();
Bank bank = new Bank("馮潤發","123456",200.0);
bank.depoist(200.0); bank.withdrawal("123456",150.0); bank.withdrawal("654321",150.0); bank.withdrawal("123456",450.0); Bank.welcomenext();}
}
···
員工類
···
package shiyan4;
public class Employee { //定義成員變數
private String empId;
private String name;
private int age;
private String sex;
//定義構造方法
public Employee(){}
//定義成員方法set,get方法
public String getEmpId(){
return empId;
}
public void setEmpId(String empId){
this.empId=empId;
}
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 getSex(){
return sex;
}
public void setSex(String sex){
this.sex=sex;
}
//定義成員方法show方法
public void show(){
System.out.println("員工編號是:"+getEmpId()+"\t姓名是:"+getName()+"\t年齡:"+getAge()+"\t性別:"+getSex());
}
}
package shiyan4;
public class Test {
public static void main(String[] args){
//建立對象
Employee e = new Employee();
e.setEmpId("河北農業大學");
e.setName("馮潤發");
e.setAge(21);
e.setSex("男");
//調用顯示資訊方法
e.show();
}
}
···
JAVA作業三