包
定义:为了便于管理大型软件系统中数目众多的类,解决类的命名冲突问题,Java引入包(package)机制,提供类的多重命名空间。
实例
package lee;
public class Hello{
public static void main(String [] args){
System.out.println("Hello World!");
}
}
package lee;
import lee.sub.Apple;
public class HelloTest{
public static void main(String [] args){
Hello hello =new Hello();
Apple a = new Apple();
}
}
package lee.sub;
public class Apple{}
编译语句:javac -d . 类名.java
这样的好处是这个类里引入的包,直接就可以将先关的.class 文件生成到相应的文件夹下。
Java常用包
Java.lang:这个包下包含了java语言的核心类,如string、Math、System和Thread类,使用这个包下面的类无需使用import语句导入,系统会自动导入这个包下的所有类。
访问控制
类的继承与权限访问
实例代码:
class Person{
private String name;
private int age;
public void setName(String name){
this.name = name;
}
public void setAge(int age ){
this.age = age;
}
public String getName(){
return name;
}
public int getAge(){
return age;
}
}
class Student extends Person{
private String school;
public String getSchool(){
return school;
}
public void setSchool(String school){
this.school = school;
}
}
public class TestPerson{
public static void main(String args[]){
Student student = new Student();
student.setName("Jason");
student.setAge(28);
student.setSchool("清华大学");
System.out.println(student.getName());
System.out.println(student.getAge());
System.out.println(student.getSchool());
}
}
输出结果:
继承中的构造方法
实例代码:
class SuperClass{
private int n;
SuperClass(){
System.out.println("SuperClass()");
}
SuperClass(int n){
System.out.println("SuperClass(" + n +")");
this.n = n;
}
}
class SubClass extends SuperClass{
private int n;
SubClass(int n){
System.out.println("SubClass(" + n +")");
this.n = n;
}
SubClass(){
super(300);
System.out.println("SubClass()");
}
}
public class TestSuperSub{
public static void main(String arg[]){
SubClass sc1 = new SubClass();
SubClass sc2 = new SubClass(400);
}
}
输出结果:
方法的重写
实例代码:
class Person{
public String name;
public int age;
public void setName(String name){
this.name = name;
}
public void setAge(int age ){
this.age = age;
}
public String getName(){
return name;
}
public int getAge(){
return age;
}
public String getInfo(){
return "My Name: " + name + "\n" + "Age: " + age;
}
}
class Student extends Person{
private String school;
public String getSchool(){
return school;
}
public void setSchool(String school){
this.school = school;
}
public String getInfo(){
return "My name: " + name + "\n" + "Age: " + age + "\n" + "My school: " + school;
}
}
public class TestPerson{
public static void main(String args[]){
Student student = new Student();
Person person = new Person ();
person.setName("小明");
person.setAge(29);
student.setName("Jason");
student.setAge(28);
student.setSchool("清华大学");
System.out.println(person.getInfo());
System.out.println(student.getInfo());
}
}
输出结果:
Object类
toString方法
实例代码:
public class TestToString{
public static void main(String [] args){
Dog d = new Dog();
System.out.println("d:=" + d);
}
}
class Dog{
public String toString(){
return "Iam a hot Dog!";
}
}
输出结果:
equals方法
实例代码:
public class TestEquals{
public static void main(String [] args){
Cat c1 = new Cat(1,2,3);
Cat c2 = new Cat(1,2,5);
System.out.println(c1 == c2);
System.out.println(c1.equals(c2));
String s1 = new String("hello");
String s2 = new String("hello");
System.out.println(s1 == s2);
System.out.println(c1.equals(c2));
}
}
class Cat{
int color;
int height;
int weight;
public Cat(int color, int height, int weight){
this.color = color;
this.height = height;
this.weight = weight;
}
public boolean equals(Object obj){
if(obj == null)return false;
else{
if(obj instanceof Cat){
Cat c = (Cat)obj;
if(c.color == this.color && c.height == this.height && c.weight == this.weight){
return true;
}
}
}
return false;
}
}
输出结果:
对象转型(casting)
动态绑定(多态)
三个必要条件:
实例代码:
class Animal{
private String name;
Animal(String name){
this.name = name;
}
public void enjoy(){
System.out.println("叫声...");
}
}
class Cat extends Animal{
private String eyesColor;
Cat(String n, String c){
super(n);
eyesColor = c;
}
public void enjoy(){
System.out.println("猫叫声...");
}
}
class Dog extends Animal{
private String furColor;
Dog(String n, String c){
super(n);
furColor = c;
}
public void enjoy(){
System.out.println("狗叫声...");
}
}
class Lady{
private String name;
private Animal pet;
Lady(String name, Animal pet){
this.name = name;
this.pet = pet;
}
public void myPetEnjoy(){
pet.enjoy();
}
}
public class TestPlo{
public static void main(String args[]){
Cat c = new Cat("Catname","blue");
Dog d = new Dog("Dogname","black");
Lady l1 = new Lady("11",c);
Lady l2 = new Lady("12",d);
l1.myPetEnjoy();
l2.myPetEnjoy();
}
}
输出结果:
抽象类
接口