Core Java (10) object and class Summary

Source: Internet
Author: User
    1. Be sure to design the data as private
    2. Data Initialization is required.
    3. Do not use too many basic data types in the class
    4. Not all domains require independent domain accessors and domain changers
    5. Use standard format for class definition; followPublic, package scope, privateIn the orderInstance method, static method, instance domain, static domain.
    6. Break down classes with excessive responsibilities
    7. Class names and methods must reflect their responsibilities
The following test Program The reload constructor is tested to call another constructor, default constructor, object initialization block, static initialization block, instance domain initialization, and other features.
Package COM. xujin; import Java. util. random; /*** @ author ginsmile * @ version 1.0 * A <code> test </code> object to test some syntax ** default constructor * overloaded constructor * instance field initialization * Static Initialization block * object initialization block **/public class test {public static void main (string [] ARGs) {employee [] Staff = new employee [2]; staff [0] = new employee ("Bob", 1000); staff [1] = new employee ("Jim ", 2000); system. out. println (staff [0]. getbonus (); // It may be 1200020.system.out.println (staff [1]. getbonus (); // may be 13000.0} class employee {// default constructor public employee () {// name initialized to "" // salary initialized to 0.0 // ID initialized to 0 // bonus initialized to 0.0} // overload constructor public employee (string name) {This. name = Name; id = nextid; nextid ++;} public employee (string name, double salary) {This (name); // call another constructor this. salary = salary;} // defines the accessors method Public String getname () {return name;} public double getsalary () {return salary;} public int GETID () {return ID;} public double getbonus () {return bonus;} // defines the modifier method public void setname (string name) {This. name = Name;} public void setsalary (double salary) {This. salary = salary;} public void setbonus (double bonus) {This. bonus = bonus;}/*** raise the salary of an employee * @ Param percent the pecentage by which to raise the salary (0.1 means 10%) * @ return void **/Public void raisesalary (double percent) {This. salary * = (1 + percent);} // defines the variable private string name = ""; // instance domain initialization private double salary; private int ID; private Static int nextid = 1; private double bonus; // static initialization block. It is only executed when the class is loaded for the first time. Static {random ran = new random (); nextid = ran. nextint (100);} // object initialization block, executed before the constructor {bonus = 1000 * nextid ;}}
Related Article

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: 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.