Java包概念

时间:2019-02-12 14:26:56

标签: java

  

Person.java

package A;
public class Person {
    public String name , surname;

    public Person() {
        name = " Unknown ";
        surname = " Unknown ";
    } 

    public Person(String n , String s) {
        name = n;
        surname = s;
    }

    public Person(Person p1) {
        name = p1.name;
        surname = p1.surname;
    }
}    
  

ContactInfo.java

 package A.B;
    import A.Person;

    public class ContactInfo extends Person {

        public String phone;

        public ContactInfo() {
             phone = "Unvalid ";
        } 

        public ContactInfo(String n , String s , String phn) {
            super(n,s);
            phone = phn;
        } 

        public ContactInfo(ContactInfo ci) {
            super(ci);
            phone = ci.phone;
        }
    }

Employee.java

package A.B.C;
import A.B.ContactInfo;
public class Employee extends ContactInfo {

    int salary;

    public Employee() {
        salary = 0;
    }

    public Employee(String n , String s , String phn ,int sal) {
        super(n,s,phn);
        salary = sal;
    } 

    public Employee(Employee e) {
        super(e);
        salary = e.salary;
    }

    public void show() {
        System.out.println("Name: "+name+surname+ " Phone: "+phone+ " Salary: "+salary);
    } 
}
  

Office.java

//import A.B.C.Employee;
  import A.B.C.*;

class Office {
    public static void main(String args[]) {

       Employee e1 = new Employee();
       System.out.println();
       e1.show();

       Employee e2 = new Employee(" John "," Snow "," 001122 ",123);
       System.out.println();
       e2.show();

       Employee e3 = new Employee(e2);
       System.out.println();
       e3.show();          
    }
}
  

与我使用此声明时在Office.java文件中一样:导入A.B.C.员工;

     

它将显示所需的输出,但是当我使用时: import   A.B.C。*; (它将显示错误)。为什么总是无法获得正确的路径?

     

需要很少的帮助来了解它。

0 个答案:

没有答案