Java第三次作業

來源:互聯網
上載者:User

標籤:tee   分享   system   stat   理解   int   tin   源碼   動物園   

《Java技術》第XX次作業(一)學習總結

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();
}
}
~~~
不能編譯,原因:造函數調用必須是建構函式中的第一個語句。將語句super("Hello.Grandparent.")”放在子類Parent的構造方法的第一句。

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()方法為子類中新增的方法,故而animal父類無法調用該方法

改後的程式:
~~~
class Animal{
void shout(){
System.out.println("動物叫!");
}
void sleep(){
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 = (Dog)animal;
dog.sleep();
Animal animal2 = new Dog();
dog = (Dog)animal2;
dog.shout();
}
}
~~~
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]
~~~
(2)那麼,程式的運行結果到底是什麼呢?利用eclipse開啟println(per)方法的源碼,查看該方法中又調用了哪些方法,能否解釋本例的運行結果?
~~~
public void println(Object x) {
String s = String.valueOf(x);
synchronized (this) {
print(s);
newLine();
}
}
~~~

(3)在Person類中增加如下方法
~~~
java public String toString(){ return "姓名:" + this.name + ",年齡:" + this.age ; }
~~~

運行結果:
姓名:張三,年齡:20
姓名:張三,年齡:20
說明了,子類Person類重複了Object類中toString()方法

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();
}
}
~~~

不能:介面中方法,預設是public abstract 所以在子類實現抽象方法時,應該用public修飾

(二)實驗總結

一個思想,一個和資料庫類似的兩層模式三級映像的模式思想

2.按照下面要求完成類的設計(1)設計一個平面圖形抽象類別(提供求該類對象周長和面積的方法)和一個立體圖形抽象類別(提供求該類對象表面積和體積的方法)(2)設計球類、圓柱類,圓錐類、矩形類、三角形類、圓類,分別繼承平面圖形抽象類別和立體圖形抽象類別。(3)建立測試類別,進行測試。畫出類圖。3.. 參考類圖,重構下面的執行個體,分析和理解多態的含義和用途(1)某動物園有一飼養員小李,每天需要給他所負責飼養的一隻獅子、五隻猴子和十隻鴿子餵食。 請用一個程式來類比他餵食的過程(2)第一次重構,引入繼承,利用抽象類別和對象多態重構程式,Animal類採用抽象類別, 合并Feeder類中的方法(3)第二次重構,修改feedAnimals方法,讓它接收一個Animal數組(三)[代碼託管][email protected]:java1601/first_java_operation.git
  • 碼雲提交曆史

Java第三次作業

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.