Java“封裝”的例子


/*
功能:Java"封裝"的典型例子
*/

public class Demo3_5
{
    public static void main(String args[]){
        Clerk clerk1=new Clerk("qingfeng",22,5321.12f);
        System.out.println("名字為"+clerk1.name+"薪資為"+clerk1.getSalary());//只能通過授權的操作(成員方法)進行訪問
        
    }
}

class Clerk
{
    public String name ;
    private int age;
    private float salary;

    //構造方法
    public Clerk(String name, int age, float sal){
        this.name = name;
        this.age = age;
        this.salary = sal;
    }
    //成員方法
    public float getSalary(){
        return this.salary;
    }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM