在一个项目中建立三个包分别为:
chapter2
pack2
pack3
package chapter2;
public class Telphone {
//属性
private float cup;
private float screen;
private float mem;
//get set方法
public float getScreen()
{
return screen;
}
public void setScreen(float newScreen)
{
screen=newScreen;
}
//方法
public Telphone()
{
System.out.println("第一个包的Telphone类");
}
}
package pack2;
public class Telphone {
public Telphone() {
// TODO Auto-generated constructor stub
System.out.println("第二个包的类Telphone");
}
}
package pack3;
public class Telphone {
public Telphone() {
// TODO Auto-generated constructor stub
System.out.println("第三个包的Telphone类");
}
}
package chapter2;
public class InitalTelphone {
public static void main(String[] args) {
//通过无参方法生成对象
Telphone tel=new Telphone();
}
}
package chapter2;
import pack2.Telphone;
public class InitalTelphone {
public static void main(String[] args) {
//通过无参方法生成对象
Telphone tel=new Telphone();
}
}
执行程序会发现结果为:
package chapter2;
import pack2.*;
public class InitalTelphone {
public static void main(String[] args) {
//通过无参方法生成对象
Telphone tel=new Telphone();
}
}
程序运行结果又发生了变化:
package chapter2;
import pack2.Telphone;
import pack3.Telphone;;
public class InitalTelphone {
public static void main(String[] args) {
//通过无参方法生成对象
Telphone tel=new Telphone();
}
}
package chapter2;
import pack2.*;
import pack3.Telphone;;
public class InitalTelphone {
public static void main(String[] args) {
//通过无参方法生成对象
Telphone tel=new Telphone();
//有参构造方法生成对象
//Telphone tel2=new Telphone(4.0f,3.0f,1.5f);
}
}
程序执行结果为: