包和繼承
- 包(package)
- 包的規范
- 匯入包
- 包的訪問權限
- 常見的系統包
- 封裝
- 繼承
- extends關鍵字
- protected關鍵字
- final關鍵字
- super關鍵字
- 父子類執行順序
包(package)
是組織類的一種方式.
使用包的主要目的是保證類的唯一性.
包的規范
一般采用域名的倒置,并且全部為小寫,例如:www.baidu.com
所以包就可以這樣設定

匯入包
用import關鍵字,后面接想要匯入的包
import java.util.Date;
public class Test {
public static void main(String[] args) {
Date date = new Date();
// 得到一個毫秒級別的時間戳
System.out.println(date.getTime());
}
}
包的訪問權限
- public
- private
- protected
- 什么都不寫(默認權限)———— 只能在同一包內進行訪問
常見的系統包
- java.lang:系統常用基礎類(String、Object),此包從JDK1.1后自動匯入,
- java.lang.reflect:java 反射編程包;
- java.net:進行網路編程開發包,
- java.sql:進行資料庫開發的支持包,
- java.util:是java提供的工具程式包,(集合類等) 非常重要
- java.io:I/O編程開發包,
封裝
什么是封裝?
將欄位或者方法使用private進行修飾,
封裝的意義在于:
安全性,讓類的呼叫者堆類的使用成本降低了,
封裝的詳解在我之前寫的類和物件那篇中
點我跳轉 類和物件
繼承
意義:減少代碼的重復使用
extends關鍵字
A extends B
表示A繼承B
此時B稱作基類/父類/超類
而A稱作子類/派生類
舉個例子
Dog 繼承 Animal
Bird 繼承 Animal
class Animal {
public String name;
public int age;
public void eat() {
System.out.println("Animal::eat()");
}
}
class Dog{
public String name;
public int age;
public void eat() {
System.out.println("Dog::eat()");
}
}
class Bird{
public String name;
public int age;
public void eat() {
System.out.println("Bird::eat()");
}
public void fly() {
System.out.println("Bird::fly()");
}
}
我們只需要讓Dog 和Bird extend Animal就可以不用再寫重復的欄位和方法了,像這樣子
class Animal {
public String name;
public int age;
public void eat() {
System.out.println(this.name + "::eat()");
}
}
class Dog extends Animal{
}
class Bird extends Animal{
public void fly() {
System.out.println("Bird::fly()");
}
}
那么問題來了,
子類繼承父類,都繼承了父類的哪些東西?
繼承了除構造方法外的所有父類的內容,包含private修飾的欄位或方法
但需要注意的是,private修飾的不能在子類進行訪問,
什么時候把關系設定為繼承?
A is B 這時候就需要用到繼承
比如上面的例子,狗是動物,鳥是動物,
但不能是狗是鳥,這樣就不需要用到繼承了,
java是單繼承的,也就是 只能extends一個類
為了解決單繼承問題,java引入了介面,在后面會繼續學習介面
protected關鍵字
剛才我們發現, 如果把欄位設為 private, 子類不能訪問. 但是設成 public, 又違背了我們 “封裝” 的初衷.
兩全其美的辦法就是 protected 關鍵字.
- 對于類的呼叫者來說, protected 修飾的欄位和方法是不能訪問的
- 對于類的 子類 和 同一個包的其他類 來說, protected 修飾的欄位和方法是可以訪問的
//在animal包中
class Animal {
protected String name;
public int age;
public void eat() {
System.out.println(this.name + "::eat()");
}
}
//在test包中
class Test extends Animal{
public void func(){
Animal animal = new Animal();
System.out.println(animal.name);//錯誤!!這樣呼叫時不行的
System.out.println(super.name);//正確,呼叫父類的需用到super關鍵字
}
}
總結訪問限定關鍵字

final關鍵字
之前我們學過final可以修飾字符
final int a = 10;
final也可以修飾類
final class A {}
這樣子表示A這個類是一個密封類,代表當前類不可以被繼承
如果以后設定類的時候不想被別的類繼承,則可以用final修飾
super關鍵字
super和this經常一起被問到,
this代表當前物件的參考
先來說說this的幾種方法
- this() 呼叫自己的構造方法
- this.func() 訪問方法
- this.data 訪問屬性
- this 代表當前物件的參考
super 表示獲取到父類實體的參考
子類繼承了父類,子類構造的時候,需要首先幫助父類進行構造
怎么幫助構造,在子類的構造方法內部,呼叫父類的構造方法
super(); //顯示呼叫父類的構造方法,所以,構造方法不是被繼承的,而是在子類顯示被呼叫的
class Animal {
public String name;
public int age;
public Animal1() {
}
public Animal1(String name){
}
public void eat() {
System.out.println(this.name + "::eat()");
}
}
class Dog extends Animal{
public Dog() {
super();
}
public Dog(String name) {
super(name);
}
}
注:super不能用在static方法中,因為static修飾的方法不借助任何物件,而super本身就代表父類物件的參考,
父子類執行順序
class Animal {
public String name;
public int age;
//靜態代碼塊
static {
System.out.println("Animal::static{}");
}
//實體代碼塊
{
System.out.println("Animal::{}");
}
public Animal() {
System.out.println("Animal()");
}
public Animal(String name){
System.out.println("Animal(String)");
}
public void eat() {
System.out.println(this.name + "::eat()");
}
}
class Dog extends Animal1{
static {
System.out.println("Dog::static{}");
}
{
System.out.println("Dog::{}");
}
public Dog() {
super();
System.out.println("Dog()");
}
public Dog(String name) {
super(name);
System.out.println("Dog(String)");
}
}
class Bird extends Animal1{
static {
System.out.println("Bird::static{}");
}
{
System.out.println("Bird::{}");
}
public Bird() {
System.out.println("Bird()");
}
}
public class Test {
public static void main(String[] args) {
Dog dog = new Dog();
System.out.println("========");
Bird bird = new Bird();
}
}

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/262195.html
標籤:java
