Java Learning Note (iv): import syntax

Source: Internet
Author: User

Import syntax is a way to find the appropriate location for a particular class for the compiler.

Create an employee class that includes four entity variable names (name), age, Position (designation), and salary (salary).

1  Public classEmployee {2 String name;3     intAge ;4 String designation;5     Doublesalary;6 7     //Constructors8      PublicEmployee (String name) {9          This. Name =name;Ten     } One  A     //Assigning a value to an entity variable -      Public voidEmpage (intempage) { -Age =Empage; the     } -  -      Public voidempdesignation (String empdesig) { -designation =Empdesig; +     } -  +      Public voidEmpsalary (intempsalary) { ASalary =empsalary; at     } -  -     //ways to print job seekers ' information -      Public voidPrintemployee () { -System.out.println ("Name:" +name); -System.out.println ("Age:" +Age ); inSYSTEM.OUT.PRINTLN ("designation:" +designation); -System.out.println ("Salary:" +salary); to     } +}

It then creates a employeetest class that creates instances of two Employee classes, and invokes methods for each object to assign values to each variable.

This imports the employee class that you just created, which is created under the AAA folder, gives the fully qualified name of the name that includes the encapsulation and the class, and the compiler can easily navigate to the source class and source code.

1 ImportAAA. Employee;2 3  Public classEmployeetest {4      Public Static voidMain (String args[]) {5Employee Empone =NewEmployee ("Jacky");6Employee Emptwo =NewEmployee ("Tom");7 8Empone.empage (26);9Empone.empdesignation ("Engineer");TenEmpone.empsalary (2000); One Empone.printemployee (); A  -Emptwo.empage (20); -Emptwo.empdesignation ("Teacher"); theEmptwo.empsalary (1000); - Emptwo.printemployee (); -     } -}

Run output:

name:jackyage:designation:engineersalary:2000.0name:tomage:designation: Teachersalary:1000.0

If Employee.java is in a sibling folder, import this step can be ignored.

Java Learning Note (iv): import syntax

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.