Object-Oriented Foundation--construction method and anonymous object

Source: Internet
Author: User
Tags float max

What is a construction method? A constructor method is called whenever an object is instantiated.
The following points should be noted in the construction method:-The name of the construction method must be the same as the class name-the declaration of a constructor method cannot have any declaration of the return value type-You cannot use return in the constructor method to return a value.
Class Person{public person () {//Declares the constructor method System.out.println ("A new person object is generated. ") ;}}; public class Consdemo01{public static void Main (String args[]) {System.out.println ("declared object: person per = null;"); person per = NULL;//The object is declared without invoking the constructor method System.out.println ("instantiated object: per = new person ();");p er = new Person ();//Instantiate Object}};

Sometimes when writing a class does not define the construction method, can also be executed, in fact, this is a Java operation mechanism, in the entire Java operation, if a class does not explicitly declare a construction method, it will automatically generate a non-parameter of what does not do anything do not do any of the paparazzi for the user to use. Similar to the following form.

Note: If you provide a constructor method for a class, you will no longer generate a parameterless, no-action construction method. If there is an exception to the new person (), the additional constructor method is added with the addition of a parameterless, no-action constructor.
Anonymous object: Anonymous is no name, in Java if an object is used only once, it can be defined as an anonymous object. The so-called anonymous object is a reference to a stack less than a normal object. As shown below:
Class Person{private String name;p rivate int;p ublic person (String n,int a) {//Declares the constructor method, initializes This.setname (n) for the properties in the class; Setage (a);} public void SetName (String n) {name = n;} public void Setage (int a) {if (a>0&&a<150) {age = A;}} Public String GetName () {return name;} public int getage () {return age;} public void Tell () {System.out.println ("name:" + this.getname () + "; Age:" + this.getage ());}}; public class Nonamedemo01{public static void Main (String args[]) {New person ("Zhang San", +). Tell ();}};
Design analysis of the class:

Class Student{private string Stuno;p rivate string name;p rivate float math;p rivate float 中文版;p rivate float computer ;p ublic Student () {}public Student (String s,string n,float m,float e,float c) {This.setstuno (s); This.setname (n); This.setmath (m); This.setenglish (e); This.setcomputer (c); public void Setstuno (String s) {Stuno = s;} public void SetName (String n) {name = n;} public void Setmath (float m) {math = m;} public void Setenglish (float e) {中文版 = e;} public void Setcomputer (float c) {computer = C;} Public String Getstuno () {return stuno;} Public String GetName () {return name;} public float Getmath () {return math;} public float Getenglish () {return 中文版;} public float Getcomputer () {return computer;} public float sum () {//SUM operation return math + 中文版 + computer;} Public float avg () {//averaging return this.sum ()/3;} public float Max () {//For highest result float max = math;//math is highest score max = Max>computer?max:computer; max = Max>english?max:englis h; return Max;} public float min () {//For lowest score float min = Math;//mathematics is the highest grade min = min<computer?min:computer; min = min<english?min:english; return min;}}; public class Exampledemo01{public static void Main (String args[]) {Student stu = null;//declaration Object Stu = new Student ("MLDN-33", " Li Xinghua ", 95.0f,89.0f,96.0f); SYSTEM.OUT.PRINTLN ("Student number:" + Stu.getstuno ()); System.out.println ("Student Name:" + stu.getname ()); System.out.println ("Math score:" + Stu.getmath ()); System.out.println ("English score:" + stu.getenglish ()); System.out.println ("Highest score:" + Stu.max ()); System.out.println ("Lowest score:" + stu.min ());}};





Object-Oriented Foundation--construction method and anonymous object

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.