Java カプセル化、エンティティ クラスを学ぶ新人

ここに画像の説明を挿入

package it.heima.loop1;

public class score {
    
    
    private  double score;

    public  void setScore(double score){
    
    
        if (score <= 100 && score >= 0){
    
    
            this.score = score;
        }else{
    
    
            System.out.println("输入错误");
        }

    }

    public double getScore(){
    
    
        return score;
    }

    public void print(){
    
    
        System.out.println(score>60? "成绩及格":"不及格");
    }
}

としても

package it.heima.loop1;

public class test {
    
    
    public static void main(String[] args) {
    
    
        score t = new score();
        t.setScore(-12);
        double result = t.getScore();
        System.out.println(result);

        t.print();

    }
}

出力

输入错误
0.0
不及格

エンティティクラス

主にセーブデータの
ここに画像の説明を挿入
ショートカット:右键-生成

ここに画像の説明を挿入

package it.heima.loop2;

public class student {
    
    
    private int age;
    private double score;

    public student() {
    
    
    }

    public student(int age, double score) {
    
    
        this.age = age;
        this.score = score;
    }

    public int getAge() {
    
    
        return age;
    }

    public void setAge(int age) {
    
    
        this.age = age;
    }

    public double getScore() {
    
    
        return score;
    }

    public void setScore(double score) {
    
    
        this.score = score;
    }
}

package it.heima.loop2;

public class oprate {
    
    
    public student studenta;
    public void studentoprate(student studenta){
    
    
        this.studenta = studenta;
    }

    public  void print(){
    
    
        if (studenta.getScore()>= 60){
    
    
            System.out.println(studenta.getAge()+"哈哈");
        }else{
    
    
            System.out.println(studenta.getAge()+"嘿嘿");
        }
    }
}

package it.heima.loop2;

public class test {
    
    
    public static void main(String[] args) {
    
    
        student s1 = new student();
        s1.setAge(18);
        s1.setScore(21);
        System.out.println(s1.getAge());
        System.out.println(s1.getScore());

        student s2 = new student();
        s2.setAge(118);
        s2.setScore(221);
        System.out.println(s2.getAge());
        System.out.println(s2.getScore());

        oprate op = new oprate();
        op.studentoprate(s1);
        op.print();

    }
}

出力結果

18
21.0
118
221.0
18嘿嘿

1. エンティティクラスとは何ですか?その特徴は何ですか?

メンバー変数はプライベートである必要があり、それらに対して get メソッドと set メソッドが提供される必要があり、パラメーターのないコンストラクターが必要です。これはデータを保存するための Java クラスであり、オブジェクトを作成したり、何かに関するデータを保存したりするために使用できます。

2. エンティティ クラスのアプリケーション シナリオは何ですか?

エンティティ クラスは、データとデータ ビジネス処理が分離される、ソフトウェア開発におけるより一般的な開発方法に対応します。

おすすめ

転載: blog.csdn.net/AdamCY888/article/details/131495227