this 关键字
1、当 成员变量 名 和 局部变量 相同时,优先使用 局部变量,通过 this 使用 成员变量。
public class Test1 {
public static void main(String[] args) {
Person p = new Person("tom");
System.out.println(p.name); //tom
}
}
class Person{
String name;
public Person() {}
public Person(String name) {
this.name = name;
}
}
2、this 用于 构造方法间的调用。
注:this 在构造方法 里 使用 时,必须 放在 第一句
public class Test2 {
public static void main(String[] args) {
Person2 p = new Person2();// 调用 类 的无参构造方法
}
}
class Person2{
public Person2() {
this(10);// 有参构造方法10
System.out.println("无参构造方法");
}
public Person2(int age) {
System.out.println("有参构造方法"+age);
}
}
继承
继承是从已有的类中派生出新的类,新的类能吸收已有类的数据属性和行为。
继承的特点:1、使用 extends 关键字
2、相当于 子类 把 父类 的功能 复制了 一份
3、java 只支持 单继承
4、继承可以 传递 (爷爷 儿子 孙子 的关系)
5、不能 继承 父类 的 私有成员
6、继承 多 用于 功能 的 修改,子类 可以 拥有 父类 的 功能 的同时,进行 功能拓展
7、继承 是 is a 的关系,是子类和父类发生了强制的依赖关系–强耦合
public class Test3 {
public static void main(String[] args) {
Fu fu = new Fu();
fu.show();
System.out.println(fu.age);
Zi zi = new Zi();
zi.show();
System.out.println(zi.age);
// System.out.println(zi.sum); // error ,不可继承 父类private修饰的成员
zi.play();
System.out.println(zi.name);
}
}
class Yeye{
String name;
public void play() {
System.out.println("下象棋");
}
}
class Fu extends Yeye{
int age;
private int sum;
public void show() {
System.out.println("show()...");
}
}
class Zi extends Fu{}
super 关键字
1、通过 super 关键字 可以 使用 父类 的 内容
2、super 代表 父类 的 一个 引用
3、在 构造方法中 调用 时,必须 放在 第一条 语句
方法重写(override)
存在 父类 与 子类 间 的,子类 定义 的 方法 与 父类 定义 的 方法 具有 相同 的 方法名、返回值类型、参数列表
1、继承 中 成员变量 的 使用
使用 父类 成员时,使用 super 进行 调用
public class Test4 {
public static void main(String[] args) {
Son s = new Son();
s.show();
}
}
class Father{
int count = 5;
int sum = 30;
}
class Son extends Father{
int sum = 10;
public void show() {
int sum = 20;
System.out.println(sum);// 20
System.out.println(this.sum);// 10
System.out.println(count);// 5
System.out.println(super.count);// 5
System.out.println(super.sum);// 30
}
}
2、继承 中 成员方法 的 使用
注:当实现 方法重写 时,子类必须有 权限 重写 父类 的 方法 ,子类 重写方法 权限 >= 父类 方法 权限
public class Test5 {
public static void main(String[] args) {
Erzi erzi = new Erzi();
erzi.eat(); // 吃素菜
erzi.play();// 下象棋
erzi.coding();// coding()...
// erzi.study();// error,不能继承 private
}
}
class Baba{
public void eat() {
System.out.println("吃肉肉");
}
private void study() {
System.out.println("study()...");
}
public void play() {
System.out.println("下象棋");
}
}
class Erzi extends Baba{
public void eat() {
// super.eat();
System.out.println("吃素菜");
}
public void coding() {
System.out.println("coding()...");
}
}
2、继承 中 构造方法 的 使用
子类 在创建 时 ,会 自动 调用 父类 的 无参构造方法
在 构造方法 的 第一行,都有 一条默认 的语句 super()
父类 没有 无参构造 时,可以使用 super 调用 父类 其他 的 构造方法
注:在 构造方法 中 使用 super 时 ,必须 放在 第一条 语句
public class Test6 {
public static void main(String[] args) {
Sub s = new Sub();
}
}
class Babby{
public Babby() {
System.out.println("babay 无参 构造...");
}
public Babby(int age) {
System.out.println("babby 有参构造"+age);
}
}
class Sub extends Babby{
public Sub() {
// super();// 默认存在这条语句,调用 父类 的无参构造
super(10);// 调用 父类 的 有参构造
System.out.println("sub 无参构造...");
}
}
static 关键字
static 是 java 中 的关键字,用于 修饰 成员 (成员变量 和 成员方法 ,不能 修饰 局部变量)
static 的 特点:1、static 能 修饰 成员变量 和 成员方法
2、static 是全局唯一的,全局共享
3、static 随 类的加载而加载,且优先于 对象加载
4、static 只加载一次,就一直存在,不再开辟 新的空间
5、static 可以直接 被 类名 调用
6、静态 只能 调用 静态,非静态 可以 随意 调用
7、static 不能 和 this 或者 super 共用,因为 有 static 时 可能还没有 对象
// 这个类 用来测试 静态static 入门案例
public class Test7_Static {
public static void main(String[] args) {
//2、静态资源 优先于对象加载
StaticDemo.game(); //3、静态资源可以 直接 被 类名 调用
StaticDemo demo = new StaticDemo();
demo.show();
System.out.println(demo.age);
//4、全局共享,可以被多个对象共享
demo.name = "jack";
StaticDemo demo2 = new StaticDemo();
System.out.println(demo2.name); // jack
System.out.println(StaticDemo.name); //静态资源 建议 用 类名 访问
}
}
class StaticDemo{
//1、static可以修饰成员变量、成员方法
static String name;
static public void game() {
System.out.println("game()...");
}
int age;
public void show() {
System.out.println("show()...");
}
}
静态 方法内存图
静态 只能 调用 静态(变量 或 方法),非 静态 可以 访问 静态或非静态
//测试 静态 的调用关系
// 总结:静态资源 只能 调用 静态资源,而普通资源 可以 随意 调用
public class Test8_Static2 {
public static void main(String[] args) {
StaticDemo2 s = new StaticDemo2();
s.eat();
s.coding();
}
}
class StaticDemo2{
//提供普通资源
int age;
public void eat() {
// 1、普通资源 可以调用 静态资源 yes
coding();
System.out.println(name);
System.out.println("eat()...");
}
static String name;
static public void coding() {
// 2、静态资源 不可以 调用 普通资源 no
// eat(); // error
// System.out.println(age); //error
System.out.println("coding()...");
}
}
静态代码块
在 成员位置(类内 方法外)随着 类 的 加载 而 加载,先于 对象 加载,并且 只被 加载一次,一般用于 项目 的初始化
static{…}
// 测试 静态代码块
public class Test9_StaticBlock {
public static void main(String[] args) {
StaticDemo3 s = new StaticDemo3();// 静态代码块 构造代码块 构造方法....
StaticDemo3 s2 = new StaticDemo3();// 构造代码块 构造方法....
StaticDemo3 s3 = new StaticDemo3();// 构造代码块 构造方法....
}
}
class StaticDemo3{
// 位置:是 在类内 方法 外,
// 是随着类的加载而加载,加载时机早于对象
// 而且只会 被 加载 一次来提高 效率,用来 完成项目的启动 或者 加载资源
static {
System.out.println("静态代码块");
}
{
System.out.println("构造代码块");
}
public StaticDemo3() {
System.out.println("构造方法");
}
public void show() {
{
System.out.println("局部代码块");
}
}
}