大學我玩夠了你呢?30天輕鬆搞定 Java 之類與對象

大學我玩夠了你呢?30天輕鬆搞定 Java

Java數據處理之 類與對象

1、類

代碼展示

static final class MyMath{//static修飾後,變爲靜態類 final修飾後,該類不可以再有子類
        String s = "歡迎使用MyMath";//成員變量
        final double MYPI = Math.PI;//成員變量,而且不可變
        public MyMath(){//空參數構造方法

        }
        public MyMath(int a,int b){//含參構造方法

        }
        void hello(){//void返回類型的方法
            System.out.println(s);
        }
        int abs(int a){//int返回類型的方法,必須返回數據,而且是int類型
            if(a>0){
                return a;
            }else {
                return -a;
            }
        }
        int max(int a,int b){
            int max = a;
            if (a < b) {
                max = b;
            }
            return max;
        }
        int min(int a,int b){
            int min = a;
            if (a < b) {
                min = b;
            }
            return min;
        }
        int add(int a, int b) {
            int sum=a;
            sum = sum + b;
            return sum;
        }
    }

“類”小結
在這裏插入圖片描述
在這裏插入圖片描述

2、對象

代碼展示

package4章類與對象;//包名
import4章類與對象..MyMath;//導入的類
public class 對象 {
    public static void main(String[] args) {
        MyMath test = new MyMath();//創建對象並使用new字符進行實例化
        //實例化的過程中,也會調用類構造方法進行初始化
        System.out.println(test.MYPI);//使用對象的變量
        test.hello();//使用對象的方法
        System.out.println("test.max(2,6)== "+test.max(2,6));
        System.out.println("test.abs(-10)== "+test.abs(-10));
        System.out.println("test.add(4,5)== "+test.add(4,5));
        test = null;//對象的銷燬
    }
}

“對象小結”
在這裏插入圖片描述

3、屬性

代碼展示

 static class Car{
        int speed;//類的變量即屬性
        final int HEIGHT;//常量 不可變屬性 所有字母應該大寫
        static int weight;//靜態變量  所有的對象共享的類變量
    } 

“屬性”小結
在這裏插入圖片描述

4、方法

代碼展示

  public static boolean isPrime(int n) {//isPrime方法,返回類型boolean,參數整型n;功能:判斷一個數是否爲素數
        boolean tag = true;
        if (n == 1) {
            tag = false;
        } else {
            for (int j = 2; j <= Math.sqrt(n); j++) {
                if (n % j == 0) {
                    tag = false;
                    break;
                }
            }
        }
        return tag;
    }
    static void fun(int begin,int end){//靜態方法,參數爲兩個整型;功能:調用isPrime方法
        int count = 0;
        for (int i = begin; i <= end; i++) {
            if (isPrime(i)) {//靜態方法fun可以調用靜態方法isPrime,不可調用實例方法
                System.out.print(i + " ");
                count++;
                if (count % 10 == 0) {
                    System.out.println();
                }
            }
        }
    }
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int begin = scanner.nextInt();
        int end = scanner.nextInt();
        scanner.close();
        System.out.println(begin+" 到 "+end+" 之間的素數分別是:");
        fun(begin,end);
        Car car = new Car(1,2,3);//創建Car對象,並實例初始化
        System.out.println("car的速度、高度、重量分別是: "
                +car.getSpeed()+" "+car.getHeight()+" "+car.getWeight());//調用方法,獲取car的屬性值
        car.setSpeed(0);//設置car的速度爲 0
    }
    static class Car{
        int speed;
        int height;
        int weight;
        public Car(){//空參構造方法
        }
        public Car(int speed,int height,int weight){//含參構造方法
            this.speed = speed;//this.speed 是對象的屬性;speed是構造方法得參數
            this.height = height;
            this.weight = weight;
        }
        public int getSpeed() {
            return speed;
        }

        public void setSpeed(int speed) {
            this.speed = speed;
        }

        public int getHeight() {
            return height;
        }

        public void setHeight(int height) {
            this.height = height;
        }

        public int getWeight() {
            return weight;
        }

        public void setWeight(int weight) {
            this.weight = weight;
        }
    }

“方法”小結
在這裏插入圖片描述

5、包

“包”小結
在這裏插入圖片描述

總結

學習從點滴開始,學到的東西可能會忘記,記得點贊收藏哦
在這裏插入圖片描述

System.out.println("我選擇滑稽取寵");
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章