12. Different scopes for access modifiers

Source: Internet
Author: User

 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.