Java修飾符

1、抽象概述

  • 不具體的,似是而非的java

  • 沒有具體實現的api

  • 好比Animal,只是對動物的大概描述多線程

    • 能吃ide

    • 能睡this

  • 具體吃啥,怎麼睡咱們無從得知,建立出來的對象意義不大spa

  • 咱們認爲這種類不該該直接建立對象,應該讓其子類建立具體的對象線程

  • 怎麼限制?作成抽象類rest

    • abstract修飾的類能夠變成抽象類對象

     

2、抽象類(重點)

2.1 定義

  • 被abstract修飾的類成爲抽象類blog

  • 沒法直接建立對象

  • 抽象類中能夠存在抽象方法

package com.qf.abs;

public class Demo01 {
public static void main(String[] args) {
// 抽象類沒法直接建立對象
// Animal animal = new Animal();
}
}

abstract class Animal{

public abstract void eat();

}

2.2 抽象類的子類和對象

  • 抽象類沒法直接建立對象,可是能夠有子類

  • 抽象類的子類繼承抽象類以後能夠獲取到抽象類中的非私有普通屬性和方法

  • 抽象類的子類若是想使用抽象類中的抽象方法,必須重寫這些方法以後才能使用

  • 若是不使用抽象方法,作成抽象類也是能夠的

package com.qf.abs;

public class Demo01 {
public static void main(String[] args) {
// 抽象類沒法直接建立對象
// Animal animal = new Animal();
Husky husky = new Husky();
System.out.println(husky.type);
husky.breath();
husky.eat();

}
}

/**
* 抽象類Animal
* 描述動物的一個類
* 只作了大概的描述,沒有具體的實現
* 須要子類繼承此類後作具體的描述
* @author Dushine2008
*
*/
abstract class Animal{

// 屬性
String type = "動物";
String section;

public abstract void eat();

public abstract void sleep();

public void breath() {
System.out.println("全部的動物都依賴氧氣存活...");
}

}


abstract class Dog extends Animal{

}

class Husky extends Dog{

@Override
public void eat() {
System.out.println("二哈喜歡和主人同吃同住...");
}

@Override
public void sleep() {
System.out.println("二哈和主人同住同吃.................");
}

}

3、抽象類和多態案例(掌握)

  • 建立抽象類Car

  • 建立Car的子類

    • Auto

    • Bus

    • Tractor

  • 在子類重寫繼承的抽象方法和本身獨有的方法

  • 使用多態的思想建立對象並調用這些方法

package com.qf.abs;

public class Demo02 {
public static void main(String[] args) {
Car car01 = new Auto();
car01.start();
car01.stop();

// 向下轉型
Auto auto = (Auto) car01;
auto.manned();

System.out.println("========================");

Car car02 = new Bus();
car02.start();
car02.stop();

// 拆箱
Bus bus = (Bus) car02;
bus.manned();

System.out.println("========================");

Car car03 = new Tractor();
car03.start();
car03.stop();

Tractor tractor = (Tractor) car03;
tractor.doFarmWork();

}
}

/**
* 車的頂層類
* 抽象類
* 方法沒有具體的實現
* @author Dushine2008
*
*/
abstract class Car{
// 屬性
int weight;
int height;
String brand;
int price;

// 方法
public abstract void start();

public abstract void stop();
}

/**
* 奧拓車
* 繼承Car
* 重寫啓動和中止的方法
* @author Dushine2008
*
*/
class Auto extends Car{

@Override
public void start() {
System.out.println("Auto擰鑰匙啓動...");
}

@Override
public void stop() {
System.out.println("Auto這個車子剎車系統是鼓剎...");
}

public void manned() {
System.out.println("Auto這輛小車能載人5個...");
}

}

/**
* Bus類
* 繼承了Car類
* 重寫抽象方法
* @author Dushine2008
*
*/
class Bus extends Car{

@Override
public void start() {
System.out.println("Bus啓動方式不少,能夠擰鑰匙,能夠人力推,還可以使用搖把啓動...");
}

@Override
public void stop() {
System.out.println("Bus剎車使用腳剎...");
}

public void manned() {
System.out.println("Bus載人數量能達到百人");
}

}
/**
* Tractor類
* 繼承了Car類
* 重寫抽象方法
* @author Dushine2008
*
*/
class Tractor extends Car{

@Override
public void start() {
System.out.println("Tractor早期使用搖把,後來升級了電啓動...");
}

@Override
public void stop() {
System.out.println("Tractor手扶拖拉機使用手剎,很拉風...");
}

public void doFarmWork() {
System.out.println("Tractor能夠在農場裏幹活...");
}

}

4、static(重點)

4.1 定義

  • static是java中的關鍵字

  • 能修飾變量、方法、內部類

5、 靜態變量(掌握)

  • 被static修飾的變量成爲靜態變量--類變量--整個類中只存在一份

  • 靜態變量被全部此類建立的對象共享,在內存中只存在一份

  • 在任意位置修改了這個變量,後面的對象獲取到的就是修改以後的結果

  • 推薦使用類名.屬性名調用

4.2.1 非靜態變量的建立和使用

package com.qf.static0;

public class Demo01 {
public static void main(String[] args) {
MyClass c01 = new MyClass();
c01.str = "我是c01中的變量str";
System.out.println(c01.str);
System.out.println(c01);

MyClass c02 = new MyClass();
c02.str = "我是c02中的變量str";
System.out.println(c02.str);
System.out.println(c02);

}
}


class MyClass{
// 屬性--實例變量--成員變量
String str;
}

4.2.2 靜態變量的建立和使用

package com.qf.static0;

public class Demo02 {
public static void main(String[] args) {
MyClass c01 = new MyClass();
c01.str = "我是c01中的變量str";
System.out.println(c01.str);
System.out.println(c01);

MyClass c02 = new MyClass();
c02.str = "我是c02中的變量str";
System.out.println(c02.str);
System.out.println(c02);

YourClass y01 = new YourClass();
YourClass.capital = "我是韓國的國都漢城";
System.out.println(YourClass.capital);
System.out.println(y01);

YourClass y02 = new YourClass();
YourClass.capital = "我是韓國的國都首爾";
System.out.println(y01.capital);
System.out.println(y02);

}
}

class YourClass{
// 被static修飾的變量成爲靜態變量--類變量--整個類中只存在一份,被全部此類建立的對象共享,在內存中只存在一份
static String capital;
}

4.2.3 內存圖

 

 

4.2.4 記錄對象建立次數

package com.qf.static0;

public class Demo03 {
public static void main(String[] args) {
System.out.println(OurClass.count);

OurClass oc1 = new OurClass();
System.out.println(OurClass.count);

OurClass oc2 = new OurClass();
System.out.println(OurClass.count);

OurClass oc3 = new OurClass();
System.out.println(OurClass.count);

OurClass oc4 = new OurClass();
System.out.println(OurClass.count);
}
}

class OurClass{
// 定義靜態的變量count,記錄被執行次數
static int count = 0;

public OurClass(){
// 構造方法每一次調用都自增一下
count++;
}
}

class User{

}

6、靜態方法(掌握)

6.1 定義

  • static修飾的方法成爲靜態方法

  • 不用建立對象就能直接調用

package com.qf.static0;

public class Demo04 {
public static void main(String[] args) {
// System.out.println(Integer.MIN_VALUE);
// 調用靜態屬性
System.out.println(MathUtil.PI);
System.out.println(MathUtil.maxInteger);
System.out.println(MathUtil.minInteger);

// 調用靜態方法
double abs = MathUtil.getAbs(-23.45);
System.out.println(abs);

double pow = MathUtil.getPow(3, 0);
System.out.println(pow);

}
}

class MathUtil{

// 屬性
static double PI = 3.141592653589793;
static int maxInteger = 2147483647;
static int minInteger = -2147483648;

// 方法
/**
* 獲取絕對值
* @param d
* @return
*/
public static double getAbs(double d) {
return d >= 0 ? d : -d;
}

/**
* 計算a的b次方的結果
* @param a
* @param b
* @return
*/
public static double getPow(double a,int b) {
int result = 1;
for (int i = 0; i < b; i++) {
result *= a;
}
return result;
}

}

6.2 靜態方法調用其餘屬性和方法

  • 靜態方法調用靜態屬性

    • 能夠

  • 靜態方法調用靜態方法

    • 能夠

  • 靜態方法調用非靜態屬性

    • 不能夠

  • 靜態方法調用非靜態方法

    • 不能夠

package com.qf.static0;

public class Demo05 {
public static void main(String[] args) {

}
}


class Stu{
// 實例屬性,對象建立的時候加載到堆內存
String name = "張三";
// 靜態屬性,類屬性,隨着類的加載,加載的時間早於實例屬性
static int age = 23;

// 實例方法,建立對象的時候加載進內存,加載的時間比靜態的晚
public String getInfo() {
// 能夠在非靜態方法中調用靜態方法和靜態變量
show();
return name + "===" + age;
}

// 靜態方法,隨着類的加載而加載,加載時間早於實例屬性,沒法在此調用實例屬性
public static void show() {
System.out.println(age);
// 在靜態方法中只能調用靜態變量和靜態方法
// getInfo();
// System.out.println(name);
}
}

6.3 靜態方法、屬性的繼承

  • 子類能夠獲取到父類中的實例屬性、方法

  • 子類能夠獲取到父類中的靜態屬性、方法

  • 子類能夠重寫父類中的實例方法

  • 子類不能重寫父類的靜態方法

package com.qf.static0;

public class Demo06 {
public static void main(String[] args) {
Man man = new Man();
man.show();
String info = man.getInfo();
System.out.println(info);

Human.type = "壯實的";
man.show();

man.type = "力氣大的";
System.out.println(Human.type);

}
}

/**
* 類Human
* 人類
* 有各類各樣的屬性和方法
* @author Dushine2008
*
*/
class Human{
// 實例屬性
String name = "李四";

// 靜態屬性,能夠被繼承
static String type = "好看的";

// 實例方法
public void show() {
System.out.println("我是" + name + ",我喜歡的類型是:" + type);
}

// 靜態方法,子類能夠調用,可是不能重寫/覆蓋
public static String getInfo() {
return "張三喜歡的類型:===" + type;
}
}

class Man extends Human{
@Override
public void show() {
System.out.println(name + ",我不單單喜歡===" + type);
}

/*@Override
public static String getInfo() {
return "張三喜歡的類型不單單是:===" + type;
}*/
}

7、代碼塊

7.1 局部代碼塊(熟練)

7.1.1 定義

  • 定義在方法中的代碼塊成爲局部代碼塊

  • 隨着所在方法的調用而執行

  • 能夠調用所在方法和類中的變量

  • 代碼塊內部也能夠定義變量,外部訪問不到

  • 適合臨時用一下的一些數據的處理

7.1.2 案例

package com.qf.block;

public class Demo01 {
static  int  num = 33;
public static void main(String[] args) {
int a = 110;

System.out.println("HelloWorld01");
System.out.println("HelloWorld02");

{
// 局部代碼塊
// 無痕瀏覽
int b = 220;
System.out.println("我是main方法中的局部代碼塊" + num);
}

System.out.println("HelloWorld03");
System.out.println("HelloWorld04");
System.out.println("HelloWorld05");

}
}

 

7.2 動態代碼塊(構造代碼塊)(掌握)

7.2.1 定義

  • 定義在類中的代碼塊,位置和構造方法、實例變量同一級

  • 每一次建立對象的時候被調用

  • 能夠完成構造方法中相同或者類似的代碼,對構造方法進行初始化

7.2.2 案例

package com.qf.block;

public class Demo02 {
public static void main(String[] args) {

Restaurant restaurant1 = new Restaurant();
//Restaurant restaurant2 = new Restaurant("");
//Restaurant restaurant3 = new Restaurant("","");

}
}

class Restaurant{

String name;
String addr;

{
// 隨着對象的建立執行,每建立一個對象就執行一次,能夠把構造方法中相同的部分寫在此處,在此完成構造方法的初始化
// 加載時間早於構造方法,加載時間和實例屬性部分前後,誰在前誰先上
System.out.println("幾位爺裏面請!!!");
System.out.println(name + "==" + addr);
}

public Restaurant() {
super();
System.out.println("空參構造方法。。。");
}

public Restaurant(String addr) {
super();
this.addr = addr;
System.out.println("1參構造方法。。。");
}

public Restaurant(String name, String addr) {
super();
this.name = name;
this.addr = addr;
System.out.println("2參構造方法。。。");
}

}

 

7.3 靜態代碼塊(掌握)

7.3.1 定義

  • 使用static修飾的代碼塊,定義在類中方法外,和變量、動態代碼塊的位置同樣

  • 類第一次加載的時候調用,整個程序週期中只執行一次

  • 能夠用來加載驅動等只執行一次的操做

7.3.2 案例

package com.qf.block;

public class Demo03 {
public static void main(String[] args) {
System.out.println("開始建立對象");
Mouse mouse1 = new Mouse();
Mouse mouse2 = new Mouse();
Mouse mouse3 = new Mouse();
Mouse mouse4 = new Mouse();
}
}

class Mouse{
// 屬性
String brand;
String color;

{
System.out.println("我是動態代碼塊...");
}

static {
System.out.println("我是靜態代碼塊================");
}

// 構造方法
public Mouse() {
super();
}

public Mouse(String brand) {
super();
this.brand = brand;
}

public Mouse(String brand, String color) {
super();
this.brand = brand;
this.color = color;
}
}

7.4 同步代碼塊(瞭解)

  • 多線程中會講解

相關文章
相關標籤/搜索