1.package
当在同一个包里面的对象相互调用其他类里面的函数,可以直接使用。
Scanner是系统类库里面的,在java.util的包里。
String在Java.lang的包里。
package是组织代码的一种形式,类的外面package。
如果不在自己的package里使用其他包里面的函数,用import来实现。
Clock.Display.java
CLock包里面的Clock.Display包里面的java.
最终包的关系转化成为文件里面的目录关系
2.一个继承的小例子
1 package Pra; 2 3 public class A { 4 protected void print(String s ){ 5 System.out.println(s); 6 } 7 A(){ 8 print("AHello"); 9 } 10 public void f(){ 11 print("A:GG"); 12 } 13 public static void main(String[] args){ 14 B b = new B(); b.f(); 15 } 16 } 17 class B extends A{ 18 B(){print("BHello");} 19 public void f(){print("B:GG");} 20 21 }
出现了问题一直没有输出,因为之前classA前面没有public,使得main函数没有作用。
3.一个重写的例子
1 package Pra; 2 3 4 class Person { 5 private String name = ""; 6 private String location = ""; 7 8 Person(String name) 9 { 10 this.name = name ; 11 this.location = "Beijing"; 12 } 13 Person(String name,String location) 14 { 15 this.name = name; 16 this.location = location; 17 } 18 public String info(){ 19 return "name: "+name+ "location: "+location; 20 } 21 } 22 class Student extends Person{ 23 private String school = ""; 24 Student(String name,String school){ 25 this(name,"Beijing",school);} 26 Student(String n,String l,String s){ 27 super(n,l); 28 this.school=s;} 29 public String info(){ 30 return super.info() + "School: "+school; 31 } 32 } 33 34 class Teacher extends Person{ 35 private String status = ""; 36 Teacher(String name,String status) { 37 this(name,"shanghai",status);} 38 Teacher(String n,String l,String s){ 39 super(n,l); 40 this.status=s; 41 } 42 public String info(){ 43 return super.info()+"status: "+status; 44 } 45 46 47 // TODO Auto-generated constructor stub 48 } 49 50 public class Text{ 51 public static void main(String[] args) { 52 // TODO Auto-generated method stub 53 Person a = new Person("fan","Shanghai"); System.out.println(a.info()); 54 Person b = new Person("qf");b.info(); System.out.println(b.info()); 55 Student c = new Student("fs", "tongji"); System.out.println(c.info()); 56 Student d = new Student("a","shnaghai","asdbf"); System.out.println(d.info()); 57 Teacher f = new Teacher("xie","but");System.out.println(f.info()); 58 } 59 }