C 實踐 五 類的學習與實踐 一

2021-10-19 08:29:21 字數 2693 閱讀 2885

題目:

1.定義乙個複數complex類,包含:①兩個實型字段,分別代表複數的實部和虛部;②過載「 +、- 」運算子,實現兩個複數的加法、減法運算;③定義乙個不帶參的dispcomplex方法,返回乙個複數形式的字串。編寫乙個測試類對complex類的功能進行驗證。要求帶異常處理。

**:complex類:

using system;

using system.collections.generic;

using system.linq;

using system.text;

using system.threading.tasks;

namespace test2

//讀屬性

set//寫屬性

}public double image

} public complex(

) public complex(double real,double image)

public static complex operator +(complex c1,complex c2)

public static complex operator -(complex c1, complex c2)

public string dispcomplex()}

}

測試類:

using system;

using system.collections.generic;

using system.linq;

using system.text;

using system.threading.tasks;

namespace test2

catch (exception e1)

console.writeline(

"輸入回車符結束程式的執行");

console.readline();}}}

執行結果:

總結:本實驗中使用了兩個類,乙個是complex,在這個類中主要實現了複數的演算法,採用運算子的過載,

需要注意的是:在這個運算子過載必須使用關鍵字operator.另乙個是測試類。

題目:編寫圓形circle類,包含建立物件、計算面積、計算周長、統計個數的操作。定義乙個圓柱體cylinder類,該類能夠計算圓柱體的表面積和體積。編寫乙個測試類對上述功能進行測試,要求帶異常處理。

**:circle類:

using system;

using system.collections.generic;

using system.linq;

using system.text;

using system.threading.tasks;

namespace test2

set}

public circle(

) public circle(double radius)

public string area(double radius)

public string zhoucahng(double radius)

public string count()}

}

cylinder類:

using system;

using system.collections.generic;

using system.linq;

using system.text;

using system.threading.tasks;

namespace test2

set}

public double height

set}

public cylinder(

) public cylinder(double radius,double height)

public static double square(double radius,double height)

public static double cube(double radius, double height)

}}

測試:

using system;

using system.collections.generic;

using system.linq;

using system.text;

using system.threading.tasks;

namespace test2

catch(exception e1)

console.writeline(

"請按回車結束");

console.readline();}}}

執行結果:

總結:分別做了3個類,乙個是圓的,乙個是圓柱的,最後是測試類,circle中在構造方法中做了乙個計數用用來實現定義了幾個物件。採用了基礎的演算法算出了圓的面積和周長的以及圓柱的表面積和體積。

學習與實踐總結五(Python)

按照規範的設計方法,乙個完整的資料庫設計一般分為以下六個階段 1.需求分析階段 分析使用者的需求,包括資料 功能和效能需求 2.概念設計階段 主要採用e r模型進行設計,包括畫e r圖 3.邏輯設計階段 通過將e r圖轉換成表,實現從e r模型到關係模型的轉換 4.物理設計階段 主要是為所設計的資料...

AutoTile 自動拼接(五) 學習與實踐

今天不講 權值檢索,考慮到後期 自動拼接 做出來 更好玩,操作更方便。所以 今天我 補充一節,網格計算與操作。具體就是這麼個效果,和地圖編輯器一樣,不過圖塊還是沒有自然的拼接,這個一定一定是 下一章 講,不會錯!然後這次我 不僅把demo 發給大家,還把原始檔給大家,因為貼 太多,很多朋友沒時間看,...

Sass學習實踐筆記(五)

sass 支援所有 css3 的 規則,以及一些 sass 專屬的規則,也被稱為 指令 directives 這些規則在 sass 中具有不同的功效,詳細解釋如下。import sass 擴充套件了 css 的 import 規則,讓它能夠引入 scss 和 sass 檔案。所有引入的 scss 和...