Java Foundation _3.5: Simple Java class

Source: Internet
Author: User

Simple Java class

A simple Java class is a form of definition of a class that is used most often in real-world development, including the use of core concepts such as classes, objects, construction methods, and private encapsulation in simple Java classes, while the basic development requirements for simple Java classes are as follows:

    • The class name must be meaningful, for example: Book, EMP;
    • All attributes within a class must be private, and the encapsulated property must provide a setter, getter;
    • Any number of construction methods can be provided in a class, but there must be a non-parametric construction method reserved;
    • No output statements are allowed in the class, and all information output must be given to the output to be tuned;
    • The class needs to provide a method for obtaining complete information about the object, tentatively: GetInfo (), and returns string data;
Developing the EMP Program class
Class Emp {//defines an employee class private int empno;                   Employee number private String ename;                 Employee name Private String job;                 Employee position private double sal;                    Base salary private Double comm; Commission public EMP () {///explicitly define a parameterless construction method} public emp (int eno, string ena, String J, double S, D                    Ouble c) {//parametric construction empno = Eno;                    Assign a value to a property ename = Ena;                            Assign a value to a property job = j;                            Assign a value to a property Sal = s;                       Assign a value to a property comm = C;    Assign value to property} public void Setempno (int e) {//Set Empno property content empno = e;    } public void Setename (String e) {//Set ename property content ename = e;    } public void Setjob (String j) {//Set job property content job = j; } public void Setsal (double s) {//Set Sal attribute content SAL = s;    } public void Setcomm (double c) {//Set Comm Property Content comm = C;    } public int Getempno () {//Get Empno Property contents return empno;    } public String Getename () {//Gets Ename property contents return ename;    } public String Getjob () {//Get job attribute contents return job;    } public double Getsal () {//Get Sal attribute contents return sal;    } public double Getcomm () {//Get Comm Property contents return comm; }/** * Get basic information about simple Java classes, information in the output * @return The string data that contains the full information of the object */public string GetInfo () {///* Get full                Information return "Employee Number:" + empno + "\ n" + "Employee Name:" + ename + "\ n" + "Employee Position:" + job + "\ n" +    "Base pay:" + sal + "\ n" + "commission:" + Comm; }}
To write a test program:
public class TestDemo {    public static void main(String args[]) {        Emp e = new Emp(7369, "SMITH", "CLERK", 800.0, 1.0);    // 实例化对象        System.out.println(e.getInfo());                        // 取得对象信息    } } 程序执行结果:雇员编号:7369雇员姓名:SMITH雇员职位:CLERK基本工资:800.0佣    金:1.0

Java Foundation _3.5: Simple Java class

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