PackageTest.po;//Student Class Public classStudent {//Properties PrivateString name;//name//set the Age property to modify different access modifiers, respectively /*public int age; int age; protected int age;*/ Private intAge//Age Private DoubleWeight//Weight//Non-parametric construction method PublicStudent () {//assign a value to an attribute when constructing a learner using the parameterless construction method This. name= "Anonymous"; This. age=18; This. weight=50; } //Construction Method Overloading: Method with parameter construction PublicStudent (String name,intAgeDoubleweight) { This. Name =name; This. Weight =weight; if(age<18 | | age>30) {System.out.println ("* * * Enter the age of:" +age+ ", the age is not legal, will reset!***"); This. age=18; }Else{ This. Age =Age ; } } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } Public intGetage () {returnAge ; } Public voidSetage (intAge ) { if(age<18 | | age>30) {System.out.println ("* * * Enter the age of:" +age+ ", the age is not legal, will reset!***"); return; } This. Age =Age ; } Public Doublegetweight () {returnweight; } Public voidSetweight (Doubleweight) { This. Weight =weight; } //Method//set Study () individually to decorate with different access modifiers//private void Study () {//Void Study () {//protected Void Study () { Public voidStudy () {SYSTEM.OUT.PRINTLN (name+ "said: Good study, day up!" "); }}
PackageTest.oop.demo;Importtest.po.Student; Public classteststudent {//to test different scopes for different access modifiers Public Static voidMain (string[] args) {Student Student=NewStudent (); //Scope of methods or properties that test different access modifiers /*Student.study (); Student.age;*/ }}
12. Different scopes for access modifiers