day13 面向對象練習

来源:https://www.cnblogs.com/-nbloser/archive/2018/01/13/8278156.html
-Advertisement-
Play Games

//1 1 public class Area_interface 2 { 3 public static void main(String[] args) 4 { 5 Rect r = new Rect(15, 12); 6 Circle c = new Circle(8); 7 System.o ...


//1

 1 public class Area_interface
 2 {
 3     public static void main(String[] args)
 4     {
 5         Rect r = new Rect(15, 12);
 6         Circle c = new Circle(8);
 7         System.out.println(r.getArea());
 8         System.out.println(c.getArea());
 9     }
10 }
11 
12 interface Areable{
13     double getArea();
14 }
15 
16 class NotValueException extends RuntimeException{
17     NotValueException(String message){
18         super(message);
19     }
20 }
21 
22 // 矩形
23 class Rect implements Areable{
24     private int length,width;
25     public Rect(int length,int width) {
26         if (length <= 0 || width <= 0){
27             throw new NotValueException("數值非法!");
28         }
29         this.length = length;
30         this.width = width;
31     }
32     public double getArea()
33     {
34         return length*width;
35     }
36 }
37 
38 // 圓形
39 class Circle implements Areable{
40     private int radius;
41     private static final double PI = 3.14;
42     public Circle(int radius) {
43         this.radius = radius;
44     }
45     public double getArea()
46     {
47         return PI*radius*radius;
48     }
49 }
View Code

//2

 1 public int search_char(char[] chs, char key)
 2 {
 3     if(chs==null){
 4         throw new IllegalArgumentException("數組為空");
 5     }
 6     for(int x = 0;x < chs.length;x++){
 7         if(key==chs[x]){
 8             return x;
 9         }
10     }
11     return -1;
12 }
View Code

//3

 1 //first method
 2 int max = 0;     //初始化為數組中的任意一個角標
 3 for(int  x = 1; x < cir.length; x++){
 4     if(cir[x].radius > cir[max].radius){
 5         max = x;
 6     }
 7 }
 8 return  cir[max].radius;
 9 
10 //second
11 double max = cir[0].radius;    //初始化為數組任意一個元素的半徑
12 for(int  x = 1; x < cir.length; x++){
13     if(cir[x].radius >max){
14         max = cir[x].radius;
15     }
16 }
17 return max;
View Code

//4

 

 1 class Person{
 2     private int age;
 3     private String name;
 4     public Person(int age, String name) {
 5         super();
 6         this.age = age;
 7         this.name = name;
 8     }
 9     
10     public void say()
11     {
12         System.out.println(name+"  "+age);
13     }
14     //下麵兩個都是通過右鍵搞定的
15     //get和set方法
16     public int getAge()
17     {
18         return age;
19     }
20     public void setAge(int age)
21     {
22         this.age = age;
23     }
24     public String getName()
25     {
26         return name;
27     }
28     public void setName(String name)
29     {
30         this.name = name;
31     }
32     //hashcode和equals方法
33     @Override
34     public int hashCode()
35     {
36         final int prime = 31;
37         int result = 1;
38         result = prime * result + age;
39         result = prime * result + ((name == null) ? 0 : name.hashCode());
40         return result;
41     }
42     @Override
43     public boolean equals(Object obj)
44     {
45         if (this == obj)
46             return true;
47         if (obj == null)
48             return false;
49         if (getClass() != obj.getClass())
50             return false;
51         Person other = (Person) obj;
52         if (age != other.age)
53             return false;
54         if (name == null) {
55             if (other.name != null)
56                 return false;
57         } else if (!name.equals(other.name))
58             return false;
59         return true;
60     }
61 }
equals方法通過eclipse自動生成

 

 

//5

1 public boolean equals(Object other){
2     if( !(other  instanceof  Circle) ){
3         throw new ClassCastException(other.getClass().getName+"類型錯誤");
4     }
5     Circle c = (Circle)other;
6     return  c.radius==this.radius;
7 }
View Code

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 小點心,顧名思義,開箱即食,拿來即用。彈窗就無需多說了,幾乎所有存在交互的頁面都會用到,一個頁面上甚至會有 N 多個彈窗。彈彈彈,彈出魚尾紋。如果從面向對象的角度去看,把彈窗看成一個類想必是極好的,與之綁定的DOM塊是它的屬性,打開和關閉是它的方法,而且這樣可以實現彈窗樣式和邏輯的分離。 ...
  • 在做宣傳品發放系統時,需求要把資料庫查詢的記錄生成表單並轉存excel文件。 在轉存的EXCEL文件中文顯示亂碼,表格和其他字元正常,檢查後發現是創建EXCEL文件打開模式不對 之前: myfile = fs.CreateTextFile(filename,true) 之後: myfile = fs ...
  • 前言 在《javascript 之執行環境-08》文中說到,當JavaScript代碼執行一段可執行代碼時,會創建對應的執行上下文(execution context)。對於每個執行上下文,都有三個重要屬性: 變數對象(Variable object,VO) 作用域鏈(Scope chain) th ...
  • 原因 今天在使用element ui時,el input組件監聽不了回車事件,如下代碼沒有想要的效果: 原因應該是element ui自身封裝了一層input標簽之後影響了事件的監聽,在element ui里有很多因為自身封裝了幾層標簽導致事件和樣式無法按想要的效果呈現,遇到後可以在網頁中查看dom ...
  • 12、浮動 特點:將當前元素脫離文檔流 float: left 即左浮動 float: right 即右浮動 註:*父與子元素,設置子元素浮動不能超出父元素的範圍 *多個元素均設置為浮動時,將自動排列(水平方向) *兄弟元素,後一個元素設置為浮動,前一個元素不浮動,後一個元素的位置不能超過前一個元素 ...
  • function getobjArr (data) { var result = []; data.HELMET.system = '系統分類' // console.log(data) $.each(data.HELMET, function (index_h, e... ...
  • 這兩天學習類、屬性、方法、對象等概念,由於原來基礎比較薄弱,沒接觸過面向對象的語言,一遍看下來,徹底懵逼了。 啃了兩天,才算是不那麼懵了。簡單記錄下。 Class是一種抽象概念,比如我們定義的Class——Student,是指學生這個概念,而實例(Instance)則是一個個具體的Student,比 ...
  • 之前我剛學的python知識點,沒有題目進行熟悉,後面的知識點會有練習題,並且慢慢補充。看到很多都是很簡單的練習,碰到複雜、需要運用的再補充吧#字元串中使用到正則表達式 #字元串裡面使用的是完全匹配 ##引入正則:模糊匹配#引入例子 ##元字元(11個) . 通配符 ^尖角符(頭) $(尾) * + ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...