Java Learning Record (Supplemental Three: Object-oriented encapsulation and inheritance)

Source: Internet
Author: User

 Object-oriented encapsulation
Package day7;
//Object-oriented encapsulation Public classEmployee {String name="Zhangsan"; Private intAge ; //Multiple method overloads PublicEmployee () {} PublicEmployee (String name,intAge ) { } PublicEmployee (intage,string name) { } //You can modify an inaccessible property in a method Public intGetage () {returnAge ; } Public voidSetage (intAge ) { if(age>=1&&age<= -){ This. Age =Age ; }Else{ This. Age = -; } } Public voidsay () {System. out. println (name); System. out. println (age); } }
Package Day7; Public classEmployeetest { Public Static voidMain (string[] args) {Employee user=NewEmployee (); //Assignment with Problem//user.age = 1000; //use encapsulation to hide attribute amounts, and add related get set methods//for external programs to callUser.setage ( -); System. out. println (User.getage ()); }}

Result diagram;

 Face Object Inheritance
Package day7_1;
//inheritance: only single inheritance!!!//subclass cannot access parent class private member//cannot inherit: The child class is not in the same package as the parent class, and the member that uses the default access permission//subclass cannot access the constructor of a parent class Public classEngineer {intNo; PrivateString name; PrivateString Entertime; PublicEngineer () {System. out. println ("the constructor of the parent class"); } Public voidShowinfo () {System. out. println ("Display Information"); }}
Package Day7_1; Public classSoftengineer extends Engineer {PrivateString type;  PublicSoftengineer () {System. out. println ("Constructors for subclasses"); }     Public voidcoding () {super.showinfo (); System. out. println ("I'm writing a bug! ."); }    //override: Returns a value type with the same method name and argument list. //access modifiers cannot be more restrictive than parent classes@Override Public voidShowinfo () {super.showinfo (); System. out. println ("I'm a software engineer ."); }}
Package Day7_1;//properties or methods exposed in the parent class can be called through the Super keyword in a subclass Public classTestengineer extends Engineer {PrivateString level;  PublicTestengineer () {System. out. println ("Constructors for subclasses"); }     Public voidTest () {System. out. println ("I'm in debug! ."); }         Public voidShowinfo () {System. out. println ("I'm a test engineer ."); }            //overloaded     Public voidShowinfo (inti) {System. out. println ("I am overloaded"); }}
Package Day7_1;  Public class Test {    publicstaticvoid  main (string[] args) {        new Softengineer ();        User1.showinfo ();         New Testengineer ();        User2.showinfo (3);}    }

Result diagram:

Abstract: Restricting instantiation
Package day7_4;
//Abstract: Restrict instantiation, so that the class can only provide inheritance, only for classes and methods, not to attribute Public Abstract classInstrument {//1: Abstract class can not be instantiated, can only play the role of inheritance//2: Abstract methods in abstract classes must be implemented by the quilt class unless the subclass is also an abstract class//3: Abstract methods must be placed in abstract classes//4: Abstract classes are not necessarily all abstract methods//5: Abstract method without method bodyString brand; String weight; Public Abstract voidPlay (); Public voidShow () {}}
Package Day7_4;//final Last, cannot allow this class or method or property to be inherited Public  classPiano extends Instrument {final String size="123"; //rewrite the play method inside the parent class@Override Public  voidPlay () {System. out. println ("I used to play."); }     PublicFinalvoidShowcompany () {System. out. println ("made in China"); }}
 package day7_4;  public  class   Violin extends instrument { int   length;  public  void   play () {System.  out . println ( "  I'm using the pull   "  
Package Day7_4;  Public class Test {    publicstaticvoid  main (string[] args) {            new Piano ();        User1.play ();         New Violin ();        User2.play ();            }}

Result diagram:

Static  : Statically method
Public
classUser {//member Properties intAge ; intheight; //Class Properties Static intWeight = -; //member Methods Public voidShow () {//User.age; Member properties and methods cannot be called directly by a classSystem. out. Print (user.weight); } Public Static voidshowweight () {System. out. println (User.weight); //non-static properties are not allowed in static methods//System.out.println (user.age); }}
Package Day7_5; Public classUsertest { Public Static voidMain (string[] args) {User User=NewUser (); User.age=Ten; User.weight= -;        User.showweight (); //Arrays.sort (arg0); //1: Properties and methods that are not modified with static are called member properties and member methods//member properties and member methods can only be called by an object//2: Properties and methods that use static adornments are called Class properties.//class properties and class methods can be called by a class//It can also be called by an object.    }}

Result diagram:

Java Learning Record (Supplemental Three: Object-oriented encapsulation and inheritance)

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.