面相对象编程

包 (package) 是组织类的一种方式.
使用包的主要目的是保证类的唯一性.

导入包中的类

使用import语句导入包(import 包名.类名)

//导入JAVA.util包的date类
import java.util.Date;
public class Test {
    public static void main(String[] args) {
        Date date = new Date();
        // 得到一个毫秒级别的时间戳
        System.out.println(date.getTime());
   }
}

导入包中的全部类

import java.util.*;
import java.sql.*;
public class Test {
    public static void main(String[] args) {
        // util 和 sql 中都存在一个 Date 这样的类, 此时就会出现歧义, 编译出错
        Date date = new Date();
        System.out.println(date.getTime());
   }
}
// 编译出错
Error:(5, 9) java: 对Date的引用不明确
  java.sql 中的类 java.sql.Date 和 java.util 中的类 java.util.Date 都匹配

建议导入时,加上类名,否则容易出现冲突

静态导入

使用 import static可以导入包中的静态的方法和字段

import static java.lang.Math.*;
public class Test {
    public static void main(String[] args) {
        double x = 30;
        double y = 40;
        // 静态导入的方式写起来更方便一些. 
        // double result = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
        double result = sqrt(pow(x, 2) + pow(y, 2));
        System.out.println(result);
   }
}

在这里插入图片描述

包访问权限

1.如果某个成员不包含 publicprivate 关键字, 此时这个成员可以在包内部的其他类使用,但是不能在包外部的类使用
2.在同一个包下的不同类可见(包括子包都不行

继承

// Animal.java 
public class Animal { 
 public String name; 
 
 public Animal(String name) { 
 this.name = name; 
 } 
 
 public void eat(String food) { 
 System.out.println(this.name + "正在吃" + food); 
 } 
} 
// Cat.java 
class Cat { 
 public String name; 
 
 public Cat(String name) { 
 this.name = name; 
 } 
 
 public void eat(String food) { 
 System.out.println(this.name + "正在吃" + food); 
 } 
} 
// Bird.java 
class Bird { 
 public String name; 
 
 public Bird(String name) { 
 this.name = name; 
 } 
 
 public void eat(String food) { 
 System.out.println(this.name + "正在吃" + food); 
 } 
 
 public void fly() { 
 System.out.println(this.name + "正在飞 ︿( ̄︶ ̄)︿"); 
 } 
}

这三个类都具备一个相同的 eat 方法, 而且行为是完全一样的.
这三个类都具备一个相同的 name 属性, 而且意义是完全一样的.
从逻辑上讲, Cat 和 Bird 都是一种 Animal (is - a 语义).
此时我们就可以让 Cat 和 Bird 分别继承 Animal 类, 来达到代码重用的效果.
此时, Animal 这样被继承的类, 我们称为父类 ,基类超类, 对于像 Cat 和 Bird 这样的类, 我们称为 子类, 派生类

和现实中的儿子继承父亲的财产类似, 子类也会继承父类的字段和方法, 以达到代码重用的效果

语法规则

class 子类 extends 父类 {
}

使用 extends 指定父类.
1.Java 中一个子类只能继承一个父类 (而C++/Python等语言支持多继承).
2.子类会继承父类的所有public 的字段和方法.
3.对于父类的 private 的字段和方法, 子类中是无法访问的.
4.子类的实例中, 也包含着父类的实例. 可以使用 super 关键字得到父类实例的引用

class Animal { 
 public String name; 
 public Animal(String name) { 
 this.name = name; 
 } 
 public void eat(String food) { 
 System.out.println(this.name + "正在吃" + food); 
 } 
} 
class Cat extends Animal { 
 public Cat(String name) { 
 // 使用 super 调用父类的构造方法. 
 super(name); 
 } 
} 
class Bird extends Animal { 
 public Bird(String name) { 
比特科技
 super(name); 
 } 
 public void fly() { 
 System.out.println(this.name + "正在飞 ︿( ̄︶ ̄)︿"); 
 } 
} 
public class Test { 
 public static void main(String[] args) { 
 Cat cat = new Cat("小黑"); 
 cat.eat("猫粮"); 
 Bird bird = new Bird("圆圆"); 
 bird.fly(); 
 } 
}

使用继承进行改进. 此时我们让 Cat 和 Bird 继承自 Animal 类, 那么 Cat 在定义的时候就不必再写 name 字段和 eat 方法.

protecte关键字

对于类的调用者来说, protected修饰的字段和方法是不能访问的
对于类的 子类 和 同一个包的其他类 来说, protected修饰的字段和方法是可以访问的

在这里插入图片描述

super关键字

定义

super 表示获取到父类实例的引用

1. 使用了 super 来调用父类的构造器

public Bird(String name) { 
 super(name); 
}

2. 使用 super 来调用父类的普通方法

public class Bird extends Animal { 
 public Bird(String name) { 
 super(name); 
 } 
 @Override 
 public void eat(String food) { 
 // 修改代码, 让子调用父类的接口. 
 super.eat(food); 
 System.out.println("我是一只小鸟"); 
 System.out.println(this.name + "正在吃" + food); 
 } 
}

在这里插入图片描述
注:
在这里插入图片描述

this与super

在这里插入图片描述

组合

继承类似, 组合也是一种表达类之间关系的方式, 也是能够达到代码重用的效果.

public class Student { 
 ... 
} 
public class Teacher { 
 ... 
} 
public class School { 
 public Student[] students; 
 public Teacher[] teachers; 
 }

多态

定义

一个引用可以表示多种状态

向上转型

父类名称 父类引用 =new 子类实例();

例子:
在这里插入图片描述
向上转型的发生情况:
在这里插入图片描述

方法覆写

定义:

在具有继承关系的类之间,子类定义了和父类完全相同的方法,这种方法就叫方法覆写

方法覆写的要求:

1.只能重写成员方法/实例方法,不能重写静态方法(static)。
2.子类方法的权限修饰等级>=父类权限。
3.方法覆写的返回值必须相同,至少是向上转型的返回值。

注:private修饰的成员方法不能覆写;包访问权限必须注意在同级目录下;

向下转型

要发生向下转型,必须先进行向上转型–(必须强制类型转换)

Animal animal = new Ca();
//向下转型
Cat cat =(Cat) animal;
animal.eat("食物");

注:
在这里插入图片描述

final关键字

1.final修饰属性,表示该属性为常量不可以被修改
2.final修饰,表示类不能有子类
3.final修饰方法,表示该方法无法被覆写

多态的意义

好处

1.降低使用者门槛
2.方便扩展(子类方法覆写)
在这里插入图片描述

抽象类

定义

普通类的超集,只是比普通累多了一些抽象方法而已
abstract定义抽象方法和抽象类

1.抽象方法所在的类一定是抽象类。子类必须覆写所有抽象方法。(子类是普通类)
2.抽象方法仍然可以有构造方法,普通方法等(普通类有的,抽象类也有),
子类实行对象实例化流程,先调用父类构造方法
3.抽象类必须有子类(final abstract不能同时出现),抽象方法必须被覆写(final abstract不能同时出现)

注:抽象类最大的意义在于,强制要求子类必须覆写抽象方法,保证多态的正确运行

要求

1.子类继承抽象类,仍满足is a原则。
2.抽象类仍然只能单继承,一个类只能继承一个抽象类。

接口

定义

Java中使用interface关键字定义接口,接口中只有全局变量和抽象方法
子类使用implements关键字实现接口

命名和使用规则

在这里插入图片描述

原则:
1.接口只有public权限,接口只有全局变量和抽象方法
在这里插入图片描述

2.接口的多实现,接口没有单继承限制,子类可以实现多个父接口子类在实现多个父接口是,使用,分隔
在这里插入图片描述

3.接口不能直接实例化,必须通过子接口来向上转型
在这里插入图片描述

4.子类若同时继承父类,实现接口
在这里插入图片描述

接口继承

接口可以继承一个接口, 达到复用的效果. 使用 extends 关键字.

interface IRunning { 
 void run(); 
} 
interface ISwimming { 
 void swim(); 
} 
// 两栖的动物, 既能跑, 也能游
interface IAmphibious extends IRunning, ISwimming { 
} 
class Frog implements IAmphibious { 
 ... 
}

通过接口继承创建一个新的接口 IAmphibious 表示 “两栖的”. 此时实现接口创建的 Frog 类, 就继续要实现 run 方法,
也需要实现 swim 方法.

接口间的继承相当于把多个接口合并在一起.

Clonable 接口

Object 类中存在一个 clone 方法, 调用这个方法可以创建一个对象的 “拷贝”. 但是要想合法调用clone 方法, 必须要先实现 Clonable 接口, 否则就会抛出 CloneNotSupportedException 异常.

class Animal implements Cloneable { 
 private String name; 
 @Override 
 public Animal clone() { 
 Animal o = null; 
 try { 
 o = (Animal)super.clone(); 
 } catch (CloneNotSupportedException e) { 

 e.printStackTrace(); 
 } 
 return o; 
 } 
} 
public class Test { 
 public static void main(String[] args) { 
 Animal animal = new Animal(); 
 Animal animal2 = animal.clone(); 
 System.out.println(animal == animal2); 
 } 
}

1.克隆产生了一个新的对象,把属性值拷贝
2.克隆接口,是一个标识接口,本身没有任何抽象方法,当一个类实现了cloneable接口时,就表示该类具备了克隆能力(JVM)

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值