Crazy Java Learning Notes Object-oriented (iii)-method property and value passing

Source: Internet
Author: User
Tags sin

The ownership of the method:
From a syntactic point of view: methods must be defined in a class

The method either belongs to the class itself (static adornment) or to an instance-- is it a class or an object? with or without static modification

When calling a method: Must have a keynote object (subject, caller)

1  Public class mathtest{2      Public Static void Main (string[] args) {3         System.out.println (Math.sin (1.57)); 4         System.out.println (Math.sin (MATH.PI/2)); 5     }6 }

1  Public classTestMethod {2 String name;3 4     //When you customize a constructor with parameters, the system no longer provides a parameterless constructor for the class5     //Note: If the programmer does not have a constructor, the system will provide a non-parametric constructor for the class by default6      PublicTestMethod (String name) {7          This. Name =name;8     }9         Ten      Public voidWalk () { OneSystem.out.println ( This. Name + "Walking"); A     } -  -      Public voidrun () { the         //TestMethod t = new TestMethod ("Old A"); -         //T.walk (); -         //method does not see the this represents who, who calls it on behalf of WHO, here is the TT call run, so this represents the TT -          This. Walk (); +System.out.println ( This. Name + "in running"); -     } +  A      Public Static voidMain (string[] args) { atTestMethod TT =NewTestMethod ("Old C"); - Tt.run ();  -     } -}

Method Value Delivery:

 The parameter passing mechanism of the Java method :"value pass/Copy Pass", which does not pass the value of the method in the past, but is passed in the form of a copy.

  method of variable number of parameters:Type ... Formal parameter name

1 classTestargs {2      Public voidAgeintAge ) {3System.out.println ("A's age is:" +Age );4     }5 6     //int ... represents a parameter that can pass in multiple int types7      Public voidTestint... ages) {8         //in this method, ages is equivalent to an array9          for(intag:ages) {TenSystem.out.println ("B's age is:" +AG); One         } A     } -  -      Public Static voidMain (string[] args) { the         //define a TA variable to point to the Testargs instance, because the methods in the class are instance methods, so you must create an instance to invoke -Testargs TA =NewTestargs (); -Ta.age (22); -System.out.println ("~~~~~~~~~~~~~~~~~~~~~"); +Ta.test (10,20,11,23,55,19,44,68); -     } +}

Attention:

A, each method can have at most one parameter with a variable number of parameters public void Test (int ... nums,string ... name) x
B, parameters with variable number must be in the last
public void Test (int ... nums,string name) x
public void Test (String name,int ... nums) √

Crazy Java Learning Notes Object-oriented (iii)-method property and value passing

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.