• 同一个包下,不同java文件中的类名不能相同,因为同一包下,不同java文件中的类是可以相互继承的!
  • 不同java类文件中的内部类(一个类中还包含着另外一个类)可以同名,但是同一文件下的public或者是默认范围的java类是不可以同名的。
  • 当类中方法的访问权限修饰符为private时,该方法只能被自己的类访问,不能被外部的类访问
  • 初始化时,若没有赋值则为默认值,具体时:数字为0、对象为null、布尔值为false、字符值为’\0’

对象的引用

  1. 引用是一个地址,它指明了对象的变量和方法的存储位置
  2. 将对象赋给变量或将其作为参数传递给方法时,实际上并没有使用对象,甚至没有使用对象的拷贝,使用的是对象的引用
  3. 例子:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    package Mypackage;
    import java.awt.Point;

    class RefTester{
    public static void main(String[] args){
    Point pt1,pt2;
    pt1=new Point(100,100);
    pt2=pt1;

    pt1.x=200;
    pt1.y=200;
    System.out.println("Point1:"+pt1.x+","+pt1.y);
    System.out.println("Point2:"+pt2.x+","+pt2.y);
    }
    }

执行结果:

mark

可以发现,pt2的值也被改变了,这是pt2=pt1是让pt2引用pt1,而不是将pt1的拷贝纸给pt2,pt1和pt2现在是指向同一个对象

如果改成:

1
2
pt1=new Point(100,100);
pt2=new Point(100,100);

结果就将是:

mark

:(不想重写代码,假设pt1和pt2现在是String类型)

  • pt2=pt1,此时用“==”来判断pt1和pt2是否相等,得到的结果是“true”,因为指向同一个对象,用“pt2.equals(pt1)”得到的结果也将是“true”,因为两个字符串值相同
  • pt1=new Point(100,100);pt2=new Point(100,100);,此时用“==”来判断pt1和pt2是否相等,得到的结果是“false”,因为它们现在在内存中不是同一个对象,用“pt2.equals(pt1)”得到的结果也将是“true”,因为两个字符串值相同

方法重写与方法重载

方法重写

  1. 子类可以定义新的特征,当子类需要修改父类的一些方法进行扩展,增大功能,程序设计者常常把这样的一种操作方法称为重写,也叫称为覆写或覆盖,重写是建立在继承关系上
  2. 所谓方法的重写是指子类中的方法与父类中继承的方法有完全相同的返回值类型、方法名、参数个数以及参数类型。这样,就可以实现对父类方法的覆盖

例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Person//定义父类
{public void print(){//父类中的方法
System.out.println( 父类Person的print方法! );
}
}
class Student extends Person//定义子类继承Person
{public void print(){//方法的重写
System.out.println( 子类Student的print方法! );
}
}
public class 0verrideExampleO1
{public static void main(String args[])
{Student s=new Student();
S.print();

}
}

运行结果:子类Student的print方法!

可见当子类重写了父类中的print()方法后,使用S调用的是子类的print()方法,如果一定要调用父类中的方法,可以用super关键字,super关键字可以从子类访问父类中的内容,如果要访问被重写过的方法,使用“super.方法名(参数列表)”的形式调用

例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Person2
{public void print () {
System.out.println( "父类Person的print方法! ");
}
}
class Student2 extends Person2
{public void print () {
super.print();//访问父类中被子类覆写过的方法
System.out.println(" 子类Student的print方法!" );
}
}
public class OverrideExample02
{public static void main(String args[])
{Student2 s=new Student2();
s.print();
}
}

运行结果:

mark

  • 被子类重写的方法不能拥有比父类方法更加严格的访问权限
  • 当父类中方法的访问权限修饰符为private时,该方法只能被自己的类访问,不能被外部的类访问,在子类是不能被重写的。如果定义父类的方法为public,在子类定义为private,程序运行时就会报错
  • 在继承过程中如果父类当中的方法抛出异常,那么在子类中重写父类的该方法时,也要抛出异常,而且抛出的异常不能多于父类中抛出的异常(可以等于父类中抛出的异常)。换句话说,重写方法一定不能抛出新的检查异常,或者比被重写方法声明更加宽泛的检查型异常。例如,父类的一个方法申明了一个检查异常IOException,在重写这个方法时就不能抛出Exception,只能抛出IOException的子类异常,可以抛出非检查异常

方法重载

  1. 方法重载是让类以统一的方式处理不同类型数据的一种手段。调用方法时通过传递给它们的不同个数和类型的参数来决定具体使用哪个方法,这就是多态性
  2. 所谓方法重载是指在一个类中,多个方法的方法名相同,但是参数列表不同,参数列表不同指的是参数个数、参数类型或者参数的顺序不同
  3. 不仅是一般的方法,构造方法也可以重载
  4. 例子:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    	Class Person {
    {String name;
    int age;
    void print(){
    System.out.println("姓名:" +name+"年龄:" +age);
    }
    void print(String a,int b){
    System.out.println("姓名:" +a+"年龄:"+b);
    void print(String a,int b,intC){
    System.out.println("姓名:"+a+"年龄:" +b+"ID号:" +c);
    }
    void print(String a,int b,doubleC){
    System.out.println("姓名:"+a+"年龄:" +b+"ID号:"+c);
    }
    }
    public class OverLoadExampleOL
    {public static void main(String[] args)
    {Personpl=new Person();
    p1.name="李明";
    p1.age=22;
    p1.print();
    p1.print("王小早",19);
    p1.print("金波",18,100325);
    p1.print("婉宁",25,110903);
    }
    }
    在上面的程序中,可以看到Person类中有多个名为 void print的方法,这就是方法的重载。执行程序,运行结果如下:
    

    mark

  5. 每个重载方法可以有不同的返回类型,只要参数列表不同就可以了
  6. 可以有不同的访问修饰符
  7. 可以抛出不同的异常

总结

mark

在面向对象程序设计的思想中,类的继承和多态性主要就是体现在子类重写父类的方法。而构造方法的重载作为方法重载的一个典型特例,可以通过重载构造方法来表达对象的多种初始化行为。灵活的运用方法重写与方法重载,不仅能减少编码的工作量,也能大大提高程序的可维护性及可扩展性

参考文章链接

注意以下这个方法重载的例子,实例方法可以用类名来作为返回类型创建!!

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import java.awt.Point;
//该类定义了一个矩形
class Box {
int x1 = 0;
int y1 = 0;
int x2 = 0;
int y2 = 0;

Box buildBox(int x1, int y1, int x2, int y2) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
return this;
}

Box buildBox(Point topLeft, Point bottomRight) {
x1 = topLeft.x;
y1 = topLeft.y;
x2 = bottomRight.x;
y2 = bottomRight.y;
return this;
}

Box buildBox(Point topLeft, int w, int h) {
x1 = topLeft.x;
y1 = topLeft.y;
x2 = (x1 + w);
y2 = (y1 + h);
return this;
}

void printBox(){
System.out.print("Box: <" + x1 + ", " + y1);
System.out.println(", " + x2 + ", " + y2 + ">");
}

public static void main(String[] arguments) {
Box rect = new Box();

System.out.println("Calling buildBox with "
+ "coordinates (25,25) and (50,50):");
rect.buildBox(25, 25, 50, 50);
rect.printBox();

System.out.println("\nCalling buildBox with "
+ "points (10,10) and (20,20):");
rect.buildBox(new Point(10, 10), new Point(20, 20));
rect.printBox();

System.out.println("\nCalling buildBox with "
+ "point (10,10), width 50 and height 50:");

rect.buildBox(new Point(10, 10), 50, 50);
rect.printBox();
}
}

结果为:

mark