繼承與Object

一.繼承

Java語言的繼承:單繼承

1.類和類之間的關系

(1)組合關系

公司和員工,學校和學生

(2)繼承關系

學生和人

二.Object類

public class Object {private static native void registerNatives();static {registerNatives();}
1.finalize()

對象被JVM回收的一定會自動調用。

~當對象已滿

while (true) {A a = new A();
}

規則:當對象沒有任何引用何其關聯

A a = new A();

a = null;

package com.ffyca.entity;public class A {public void finalize() throws Throwable {System.out.println("我悄悄的走了,不帶走一片樹葉...");}
}
package com.ffyca.Test;import com.ffyca.entity.A;public class TestA {public static void main(String[] args) {A a = new A();a = null;System.gc();}
}
2.toString()

Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method.

System.out.println(c1);
System.out.println(c1.toString());
3.hashcode()

對象的一個唯一值,用來快速定位內存地址

Returns a hash code value for the object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap. The general contract of hashCode is: Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application. If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result. It is not required that if two objects are unequal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables. As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the Java? programming language.)
4.==

判斷兩個引用是否指向同一個對象。

5.equals
public boolean equals(Object obj) {return (this == obj);}

判斷內容相等

==比較的是字符串的地址,但因為有字符串常量池的存在,故str1,str2指向的地址是相同的,所以返回true

如果是以new String("java")的方式創建對象,那么無論內容是否相等,==判斷返回的都是false

改寫equals()

寫一個汽車類:汽車牌號,品牌,價格,顏色

我們認為:如果兩個汽車的牌號相同那么汽車相等

a.equals(b)

?Tip:盡量不要手寫equals和hashcode

?

package com.ffyca.entity;import java.util.Objects;public class Car {private String num;private String brand;private double price;private String color;public String getNum() {return num;}public void setNum(String num) {this.num = num;}public String getBrand() {return brand;}public void setBrand(String brand) {this.brand = brand;}public double getPrice() {return price;}public void setPrice(double price) {this.price = price;}public String getColor() {return color;}public void setColor(String color) {this.color = color;}public Car(String num, String brand, double price, String color) {this.num = num;this.brand = brand;this.price = price;this.color = color;}@Overridepublic boolean equals(Object obj) {Car c2 = (Car)obj;String s1 = this.getNum();String s2 = c2.getNum();return s1.equals(s2);}@Overridepublic String toString() {return "Car{" +"num='" + num + '\'' +", brand='" + brand + '\'' +", price=" + price +", color='" + color + '\'' +'}';}
}
package com.ffyca.Test;import com.ffyca.entity.Car;public class TestCar {public static void main(String[] args) {Car car1 = new Car("陜F12345","BWM",2000000.0,"綠色");Car car2 = new Car("陜F12345","BWM",2000000.0,"綠色");System.out.println(car1.equals(car2));}
}

四.final關鍵詞

1.修飾引用

c永遠綁定此對象

final Car c = new Car();

2.變量

a永遠是3

final int a = 3;

3.屬性

只能通過構造器,或者屬性直接給值

public class E{private final int a;
}

4.class

不能被繼承

public final class E {
}

5.方法

子類,實現類不能重寫的方法

public final void run(){}

五.接口企業中的應用

1.定規則

2.常量

public interface Constants {int SUNDAY = 7;int LAST_MONDAY = 1;
}

六.String

1.包

java.lang包下的類會自動引用

2.類

3.內部實現

4.StringBuilder

提高字符串的拼接能力

StringBuilder sb = new StringBuilder();
sb.append("hello");long s1 = System.currentTimeMillis();
for (int i=0;i<1000000;i++){sb.append(" world");
}
long s2 = System.currentTimeMillis();
System.out.println("時間:"+(s2-s1)+"ms")

5.String常用的api

(1)將基本類型轉換為字符串

Integer.parseInt(str)

Double.parseDouble(str)

char[] s = {'s','a','p','t'};String t = String.valueOf(s,0,3);  System.out.println(t);

(2)字符大小轉換,空格處理

(2.1)空格處理

輸入時不小心多輸入空格

username: admin

過濾掉前后空格 trim()

(2.2)大小轉換

        String str ="apple";str = str.toUpperCase();System.out.println("str:"+str);

3.字符串截取

substring(int,int);

包頭不包尾,索引從0開始

substring(int);

substring(int,int);的重載方法,從當前索引位置一直截取到字符串的末尾

  String str = "1000-趙云-蜀國-武將";  System.out.println(str.substring(8));      System.out.println(str.substring(8, 10));

4.查找子字符串

5.拆分字符串

把字符串按照某個指定內容分割成多個字符串,返回一個字符串數組

6.startwith

public class StringTest06 {public static void main(String[] args) {String str = "hello,this is beautiful HanZhong";String str2 = "xuexiao.jpgx";        System.out.println(str.startsWith("this"));        if(str2.endsWith(".jpg")||str.endsWith(".png")||str.endsWith(".bmp")){            System.out.println("是圖片");}else {System.out.println("其它文件");}}
}

7.替換

8.正則表達式

(1)[a-z]

a-z的其中一個字符

(2)[0-9]

[0-9]的一個數字

[0-9]{17}[0-9Xx]

(3){n}

前面的數據出現的次數

[0-9Xx]:數字0-9或者X或者x

(4){m,n}

9.charAt()

獲取某個索引位置的字符

String str = "apple";
System.out.println(str.charAt(3));//"p"

10.contains()

判斷字符串中是否包含str

public class StringTest03 {public static void main(String[] args) {String str = "01234567";System.out.println(str.contains("2"));}
}

11.toCharArray()

將字符串轉換為字符數組

public class StringTest03 {public static void main(String[] args) {String str = "01234567";char[] chars = str.toCharArray();for (int i = 0;i < chars.length;i++){System.out.println(chars[i]);}}
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/bicheng/18515.shtml
繁體地址,請注明出處:http://hk.pswp.cn/bicheng/18515.shtml
英文地址,請注明出處:http://en.pswp.cn/bicheng/18515.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

FPGA時鐘:驅動數字邏輯的核心

一、引言 在FPGA&#xff08;現場可編程門陣列&#xff09;設計中&#xff0c;時鐘信號是不可或缺的關鍵要素。時鐘信號作為時序邏輯的心跳&#xff0c;推動著FPGA內部各個存儲單元的數據流轉。無論是實現復雜的邏輯運算還是處理高速數據流&#xff0c;都需要精確的時鐘信號來保…

Vanna使用ollama分析本地MySQL數據庫

上一章節中已經實現了vanna的本地運行&#xff0c;但是大模型和數據庫都還是遠程的&#xff0c;因為也就沒辦法去訓練&#xff0c;這節一起來實現vanna分析本地mysql數據庫&#xff0c;因為要使用本地大模型&#xff0c;所以開始之前需要給本地安裝好大模型&#xff0c;我這里用…

WPF/C#:理解與實現WPF中的MVVM模式

MVVM模式的介紹 MVVM&#xff08;Model-View-ViewModel&#xff09;是一種設計模式&#xff0c;特別適用于WPF&#xff08;Windows Presentation Foundation&#xff09;等XAML-based的應用程序開發。MVVM模式主要包含三個部分&#xff1a;Model&#xff08;模型&#xff09;、…

期權具體怎么交易詳細的操作流程?

期權就是股票&#xff0c;唯一區別標的物上證指數&#xff0c;會看大盤吧&#xff0c;交易兩個方向認購做多&#xff0c;認沽做空&#xff0c;雙向t0交易&#xff0c;期權具體交易流程可以理解選擇方向多和空&#xff0c;選開倉的合約&#xff0c;買入開倉和平倉沒了&#xff0…

【Spring Cloud】API網關

目錄 什么是API網關為什么需要API網關前言問題列表 API網關解決了什么問題常見的網關解決方案NginxLuaSpring Cloud Netflix ZuulSpringCloud Zuul的IO模型弊端 Spring Cloud Gateway 第二代網關——GatewayGateway的特征Spring Cloud Gateway的處理流程Spring Cloud Gateway的…

數據集要點和難點以及具體應用案例

數據集(Data set),又稱為資料集、數據集合或資料集合,是一種由數據所組成的集合。它通常以表格形式出現,其中每一列代表一個特定變量,每一行對應于某一成員的數據集的問題。數據集列出的價值觀為每一個變量,如身高和體重的一個物體或價值的隨機數,每個數值被稱為數據資…

我的又一個神奇的框架——Skins換膚框架

為什么會有換膚的需求 app的換膚&#xff0c;可以降低app用戶的審美疲勞。再好的UI設計&#xff0c;一直不變的話&#xff0c;也會對用戶體驗大打折扣&#xff0c;即使表面上不說&#xff0c;但心里或多或少會有些難受。所以app的界面要適當的改版啊&#xff0c;要不然可難受死…

Android Surface對應的Buffer怎么傳遞給HWC

Android Surface對應的Buffer怎么傳遞給HWC 引言 因為要預研Android Video overlay&#xff0c;需要將SurfaceView對應的GraphicBuffer從drm_hwcomposer中剝離出來&#xff0c;這就需要們了解SurfaceView對應的GraphicBuffer的前世今生&#xff0c;以及它的數據流向以及在各個…

輕兔推薦 —— vfox

簡介 vfox 是一個跨平臺且可擴展的版本管理工具&#xff0c;終于有一個可以管理所有運行環境的工具了 - 支持一鍵安裝 Java、Node.js、Flutter、.Net、Golang、PHP、Python等多種環境 - 支持一鍵切換不同版本 特點 支持Windows(非WSL)、Linux、macOS! 支持不同項目不同版本、…

(四)事件系統

視頻鏈接:尚硅谷2024最新版微信小程序 文章目錄 事件綁定和事件對象事件分類以及阻止事件冒泡事件傳參-data-*自定義數據事件傳參-mark 自定義數據事件綁定和事件對象 小程序中綁定事件與在網頁開發中綁定事件幾乎一致,只不過在小程序不能通過 on 的方式綁定事件,也沒有 cli…

C# 9.0的init訪問器

不控制可變性 下面是我們最常見的屬性聲明方式&#xff0c;允許屬性在類的內部和外部都可以讀取和修改 public int Id { get; set; }namespace Demo {public class Company{public int Id { get; set; }public Company(){}public Company(int id){Id id; // 可以在構造函數中…

22.Volatile原理

文章目錄 Volatile原理1.Volatile語義中的內存屏障1.1.volatile寫操作的內存屏障1.1.1.StoreStore 屏障1.1.2.StoreLoad 屏障 1.2.volatile讀操作的內存屏障1.2.1.LoadStore屏障1.2.2.LoadLoad屏障 2.volatile不具備原子性2.1.原理 Volatile原理 1.Volatile語義中的內存屏障 在…

用于生成 Avatar 的文本引導式情感和運動控制-InstructAvatar

網址 https://wangyuchi369.github.io/InstructAvatar/ 用于生成 Avatar 的文本引導式情感和運動控制 官網翻譯 最近的會說話的頭像生成模型在實現與音頻的真實和準確的嘴唇同步方面取得了長足的進步&#xff0c;但在控制和傳達頭像的詳細表情和情感方面往往存在不足&#…

APM2.8如何做加速度校準

加速度的校準建議準備一個六面平整&#xff0c;邊角整齊的方形硬紙盒或者塑料盒&#xff0c;如下圖所示&#xff0c;我們將以它作為APM校準時的水平垂直姿態參考&#xff0c;另外當然還需要一塊水平的桌面或者地面 首先用雙面泡沫膠或者螺絲將APM主板正面向上固定于方形盒子上&…

JavaScrip原型對象

參考 JavaScrip原型對象 | LogDicthttps://www.logdict.com/archives/javascripyuan-xing-mo-shi

每天寫兩道(二)LRU緩存、

146.LRU 緩存 . - 力扣&#xff08;LeetCode&#xff09; 請你設計并實現一個滿足 LRU (最近最少使用) 緩存 約束的數據結構。 實現 LRUCache 類&#xff1a; LRUCache(int capacity) 以 正整數 作為容量 capacity 初始化 LRU 緩存int get(int key) 如果關鍵字 key 存在于緩存…

如何使用Python和大模型進行數據分析和文本生成

如何使用Python和大模型進行數據分析和文本生成 Python語言以其簡潔和強大的特性&#xff0c;成為了數據科學、機器學習和人工智能開發的首選語言之一。隨著大模型&#xff08;Large Language Models, LLMs&#xff09;如GPT-4的崛起&#xff0c;我們能夠利用這些模型實現諸多…

Revit——(2)模型的編輯、軸網和標高

目錄 一、關閉縮小的隱藏窗口 二、標高&#xff08;可創建平面&#xff0c;其他標高線復制即可&#xff09; 三、軸網 周圍的四個圈和三角表示四個里面&#xff0c;可以移動&#xff0c;不要刪除 一、關閉縮小的隱藏窗口 二、標高&#xff08;可創建平面&#xff0c;其他標…

計算機體系結構期末快速復習

文章目錄 前言CPI&#xff0c;MIPS&#xff08;大題1&#xff09;加速比&#xff08;大題2&#xff09;流水線&#xff08;大題3&#xff09;CRAY-1向量機&#xff08;大題4&#xff09;Tomasulo算法&#xff08;大題5&#xff09;概念簡答題計算機系統結構的經典定義什么是透明…

深入分析 Android Activity (二)

文章目錄 深入分析 Android Activity (二)1. Activity 的啟動模式&#xff08;Launch Modes&#xff09;1.1 標準模式&#xff08;standard&#xff09;1.2 單頂模式&#xff08;singleTop&#xff09;1.3 單任務模式&#xff08;singleTask&#xff09;1.4 單實例模式&#xf…