1 构造方法(Constructor)
1.构造方法和字段、方法一样,都是类中的成员之一;
2.构造方法的结构和普通方法类似,除了下面几点:
a.构造方法的名字和类名一致(包括大小写);
b.构造方法没有返回值类型(返回值类型就是这个类型本身);
c.构造方法内部不需要返回任何的数据(实际效果是调用后把创建好的对象返回)。
1.1 构造方法的特点
1.每一个类中至少有一个构造方法;
2.如果没有显示的构造方法,那么存在一个隐式的无参数的构造方法;
3.如果一个类中有显示的构造方法,那么隐式的就不存在了。
public class Student1 {String name;int age;// 显示的构造方法Student1() {System.out.println("构造方法");}}
public class StudentTest1 {public static void main(String[] args) {// 创建一个对象Student1 s = new Student1(); // 输出结果为构造方法} }
1.2 构造方法的作用
1.调用构造方法就可以创建一个对象;
2.可以在创建对象的时候给对象中的字段初始化值。
public class Student2 {String name;int age;Student2() {System.out.println("显示的构造方法");}// 有参构造方法Student2(String s, int a) {name = s;age = a;}}
public class StudentTest2 {public static void main(String[] args) {// 字段初始化方式一Student2 s = new Student2(); s.name = "小王";s.age = 20;System.out.println(s.name + "今年" + s.age + "岁"); // 小王今年20岁// 直接在创建对象时赋值Student2 s1 = new Student2("小明", 20); // 调用了有参数的构造方法System.out.println(s1.name+"今年"+s1.age+"岁"); // 小明今年20岁}}
2 static关键字
static是Java的一个关键字,也是一个修饰符;可以修饰字段和方法,不能修饰外部类和局部变量。被static修饰的字段和方法在jvm加载类的过程中就会被初始化,并且只会初始化一次。
static修饰的方法中,不能有this出现。
2.1 静态和非静态成员(字段,方法)的访问方式
字段
1.静态字段:(字段所在的类)类名.字段名
public class Student {static int age = 10;}
public class StudentTest {public static void main(String[] args) {System.out.println(Student.age);}}
2.非静态字段:(字段所在的类的对象)对象名.字段名
public class Student {int age = 10;}
public class StudentTest {public static void main(String[] args) {System.out.println(new Student().age);}}
方法
1.静态方法:(方法所在的类)类名.方法名()
public class Student {static void eat() {};}
public class StudentTest {public static void main(String[] args) {Student.eat();}}
2.非静态方法:(方法所在的类的对象)对象名.方法名(….)
public class Student {void eat() {};}
public class StudentTest {public static void main(String[] args) {new Student().eat();}}
2.2 static的应用
1.静态字段:定义全局常量时使用。
用public static final修饰的一个成员变量
例如:Math.PI,Integer.MAX_VALUE等常量。
2.静态方法:工具类中的工具方法。
例如:Arrays中的所有方法全部都是静态修饰。
3 this关键字
this:表示对调用方法的对象的引用,哪个对象调用this所在的成员,this就表示谁。this只能在方法内部使用。
public class Person {private String name;public void setName(String name) {// this代表new Person()对象,将name值("小明")赋给这个对象的namethis.name = name;}public String getName() {// 也可以this.namereturn name; }}
public class PersonTest {public static void main(String[] args) {// 创建对象Person person = new Person(); // 赋值person.setName("小明"); System.out.println(person.getName());}}
this的用法
1.决成员变量和参数之间的二义性,如get、set方法中使用;
2.同类中构造方法重载的互调,this()必须写在第一行;
public class Person {private String name;private int age;// 构造方法public Person(String name) { this.name = name;}public Person(String name, int age) {// this(参数)调用上面的构造方法this(name); this.age = age;}}
this():表示调用同类中无参的构造方法
this(…):表示调用同类中具有指定参数的构造方法
3.作为返回值返回当前对象的引用;
public class Leaf{int i = 0;Leaf increment(){i++;// 返回当前对象的引用return this;}// 打印ivoid print(){System.out.println(i);}public static void main(String[] args) {Leaf l =new Leaf();// 返回当前对象的引用,可以重复调用方法l.increment().increment().increment().print();// 结果为3}}
4.作为参数传递。
class A {static B a(B b){return b;}}
// B类中需要调用A类中的a方法,并且参数为自身,可以用this来传递。class B {B getA(){return A.a(this);}}
4 final关键字
final:java中的关键字,表示不可变的。final也是一个修饰符,可以修饰类、方法、成员变量和局部变量。
final修饰类
使用final修饰类,表示该类是不可被继承的类,比如String,Integer等包装类。
public final class A {} // 类用final修饰,不能再有子类继承
public class B extends A {} // 编译报错The type B cannot subclass the final class A
final修饰的类中的方法都隐式的指定为final,因为无法继承覆写它们。
final修饰方法
父类中final修饰的方法不可被覆写。
public class A {final void t(){} // 方法用final修饰,不能被覆写 }
public class B extends A {void t(){} // 编译报错Cannot override the final method from A}
final修饰变量
1.修饰基本类型变量:表示该变量的值不能改变,不能用等号重新赋值;
2.修饰引用类型变量:表示该变量的引用地址不能变,而不是引用地址中的内容不能变。
public class Dragon {String name;Dragon(String name) {this.name = name;}}
public class Test {public static void main(String[] args) {// 对象中d地址不能被改变final Dragon d = new Dragon("小龙"); // 对象中的内容可以改变d.name = "纳什男爵"; System.out.println(d.name);//纳什男爵}}
3.全局静态常量:用 public static final修饰的。
如Math类中:public static final double PI = 3.141592653。
4.修饰参数:如果参数为基本数据类型,表示参数值无法修改,只能读取;如果参数为引用类型,表示参数的引用地址不能改变。
class FinalTest{// 修饰基本数据类型参数public void testInt(final int param1) {param1 = 100;// 编译报错:The final local variable param1 cannot be assigned}// 修饰引用数据类型参数public void testFinal(final Object param2) {param2 = new Object();// 编译报错:The final local variable param2 cannot be assigned}}
5 super关键字
super:表示当前对象的父类对象。
super的使用
1.通过super访问父类对象中的非私有的字段和方法。
// 父类public class User {String name = "大龙";}
public class Student extends User {String name = "小龙";Student() {System.out.println(this.name);// 访问了父类中的字段nameSystem.out.println(super.name); }}
public class SuperTest {public static void main(String[] args) {Student s = new Student();// 小龙 大龙}
2.在子类构造方法中访问父类的构造方法:
super():调用父类中的无参构造方法;
super(…):调用父类中有一个有参数的构造方法;
该方法必须放在子类构造方法的第一句。
public class User {// 父类String name;User(String name) {this.name = name;}}
public class Student extends User {int age;Student(String name, int age) {// 调用了父类中User(String name)方法,从而赋值super(name); this.age = age;}}
public class SuperTest {public static void main(String[] args) {User u = new User("大龙");Student s = new Student("小龙", 20);System.out.println(u.name + "," + s.name + "," + s.age); // 大龙,小龙,20}}
3.特殊情况:在子类中的构造方法里面存在默认(隐式)的调用父类的无参数构造方法super()。
public class User {// 无参构造方法,被子类中隐式构造方法super()调用public User() { System.out.println("大龙");}// 有参构造方法,若调用此方法,则取消调用无参构造方法public User(int i) { System.out.println("小龙");}}
public class Student extends User {Student() {// 子类的构造方法中存在隐式调用父类无参构造方法super()System.out.println("纳什男爵");}}
public class SuperTest {public static void main(String[] args) {new Student();// 会创建一个父类和一个子类(大龙,纳什男爵)}}
与this比较
一个构造方法中调用另一个构造方法用this;子类构造方法中调用父类构造方法用super。