Java object-oriented-1

Source: Internet
Author: User

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/8A/9C/wKioL1g1ZkKQL7BnAAAqq1t6FqE071.png "title=" 1.png " alt= "Wkiol1g1zkkql7bnaaaqq1t6fqe071.png"/>

The code and notes are as follows:

CLASS PERSON{//member variable string name;integer age;//static variable static string address= "China";//construction method// Two constructor methods in the same class, but also the same name, only the parameter is different//This method phenomenon, is the method overload. If there is a method of the parent class, the subclass also has an identical, even//parameter method, just the statement (method code block) that implements this method//different, this method phenomenon, is the method rewrite. Public person () {///default construction method/* The default constructor is not writable, but when you write a custom construction method, the default constructor method is overwritten. That is, if you write the following construction method, but disdain this empty construction method so: Person p = new person (); This statement will go wrong because New person () Represents a constructor method that calls an empty argument. To succeed is to write: Person p = new person ("Lisi", 23); This statement calls the two arguments and the first is the string type, the second is the integer type. */}public person (string name,integer age) {//with parameter custom construction method/* Where the This pointer represents this object, for example: when we want to use the age and name variables in the class, This is used: Person p=new person ();p. Name= "Zhangsan";p. Age=25, and this refers to the instance when the class generates an instance, the above example represents the object p;*/ SYSTEM.OUT.PRINTLN ("Custom construction Method"); this.name=name;this.age=age;} Member Method Public void eat () {System.out.println ("eat ...");} Public void toprint () {System.out.println ("name=" +this.name+ "\nage=" +this.age);} static method Public static void staticfun () {SYSTEM.OUT.PRIntln ("static method ....");} Class main{public static void main (String[] args) {person person = new  person (); System.out.println ("*************"); Person person1 = new person ("Zhangsan", 25); System.out.println ("*************");p erson.name= "Lisi";p erson.age=12;person.eat ();p erson.toprint (); System.out.println ("*************");p erson1.eat ();p erson1.toprint ();/* Static is a representation of a method or variable modified by static, etc., belonging to a class, that is, This is not required for static modification: Person person = new person ();p erson.staticfun (); Instead, it can be accessed directly through the class: Person.staticfun (); *///the first four sentences, is the traditional access mode//After two sentences are directly accessed through the class. Person.staticfun (); System.out.println (person.address);p erson1.staticfun (); System.out.println (person1.address); Person.staticfun (); System.out.println (person.address);}}

Run:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/8A/A0/wKiom1g1Zq3DxeQFAAAOkhfywf4630.png "title=" Untitled. png "alt=" Wkiom1g1zq3dxeqfaaaokhfywf4630.png "/>

This article is from the "http://sunshine2624.blog.51cto.com/3959438/1875911" blog, please be sure to keep this source.

Java object-oriented-1

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.