一、继承
1、类与类之间常见关系(整体与部分的关系)
//球员类
class Player{
int num; //编码
String name;
public Player(int num , String name){
this.num = num;
this.name = name;
}
public void run(){
System.out.println(name+"开跑...");
}
}
//球队类
class Team{
String name; //球队的名字
Player p1; //球员1
Player p2; //球员2
Player p3; //球员3
public Team(String name,Player p1,Player p2,Player p3){
this.name = name;
this.p1 = p1;
this.p2 = p2;
this.p3 = p3;
}
//开始比赛
public void startGame(){
System.out.println(name+"开赛啦!!");
}
}
class Demo10.1{
public static void main(String[] args){
Player p1 = new Player(12,"梅西");
Player p2 = new Player(7,"C罗");
Player p3 = new Player(11,"内马尔");
//球队
Team t = new Team("恒大",p1,p2,p3);
t.startGame();
System.out.println("名字:"+ t.p2.name);
}
}
2、继承:继承是通过关键字extends体现的。
3、继承的格式
class 类名1 extends 类名2{
}
4、继承要注意的事项:
① 不要为了减少重复代码而去继承,只有真正存在着继承关系的时候才去继承
② 父类私有的成员不能被继承。
③ 父类的构造函数不能被继承
④ 创建子类对象时默认会先调用父类无参的构造函数
//人类
class Person{
String name;
private int age;
public Person(String name){
this.name = name;
}
public Person(){
System.out.println("Person类的构造方法被调用了....");
}
public void eat(){
System.out.println(name+"在吃饭...");
}
}
//学生类
class Student extends Person { // Student称作为Person类的子类,Person类就称作为Student的父类(超类、基类)
int num; //学号
public Student(){
System.out.println("Student类的构造方法被调用了....");
}
public void study(){
System.out.println(name+"好好学习!");
}
}
class Demo10.2{
public static void main(String[] args){
Student s = new Student();
s.name = "狗娃";
System.out.println("名字:"+ s.name);
s.eat();
}
}
5、调用父类的构造方法是可以初始化从父类继承下去的属性的。
class Fu{
int x = 10;
String name;
public Fu(String name){
this.name = name;
System.out.println("Fu类d带参的构造方法...");
}
public Fu(){
System.out.println("Fu类无参的构造方法...");
}
}
class Zi extends Fu{
int x = 20;
public Zi(String name){
super(name); //指定调用父类一个参数的构造函数。
}
public void print(){
System.out.println("x1 = "+ x);
}
}
class Demo10.3{
public static void main(String[] args){
Zi z = new Zi("大头儿子");
System.out.println("name= "+z.name);
}
}
6、super关键字
① super关键字代表了父类空间的引用。
② super关键字的作用
◆ 子父类存在着同名的成员时,在子类中默认是访问子类的成员,可以通过super关键字指定访问父类的成员。
◆ 创建子类对象时,默认会先调用父类无参的构造方法,可以通过super关键字指定调用父类的构造方法。
③ super关键字调用父类构造方法要注意的事项
◆ 如果在子类的构造方法上没有指定调用父类的构造方法,那么java编译器会在子类的构造方法上面加上super()语句。
◆ super关键字调用父类的构造函数时,该语句必须要是子类构造函数中的第一个语句。
◆ super与this关键字不能同时出现在同一个构造函数中调用其他的构造函数。因为两个语句都需要第一个语句。
④ super关键字与this关键字的区别
◆ 代表的事物不一致。
super关键字代表的是父类空间的引用。 this关键字代表的是所属函数的调用者对象。
◆ 使用前提不一致。
super关键字必须要有继承关系才能使用。this关键字不需要存在继承关系也可使用。
◆ 调用构造函数的区别
super关键字是调用父类的构造函数。this关键字是调用本类的构造函数。
7、方法的重写
① 方法重写的前提: 必须要存在继承的关系。
② 方法的重写:子父类出了同名的函数,这个我们就称作为方法的重写。
③ 什么是时候要使用方法的重写:父类的功能无法满足子类的需求时。
④ 方法重写要注意的事项:
◆ 方法重写时, 方法名与形参列表必须一致。
◆ 方法重写时,子类的权限修饰符必须要大于或者等于父类的权限修饰符。
◆ 方法重写时,子类的返回值类型必须要小于或者等于父类的返回值类型。
◆ 方法重写时,子类抛出的异常类型要小于或者等于父类抛出的异常类型。
Exception(最坏)、RuntimeException(小坏)
⑤ 方法重载的要求
◆ 函数名要一致。
◆ 形参列表不一致(形参的个数或形参的类型不一致)
◆ 与返回值类型无关。
class Animal{} //大的数据类型
class Fish extends Animal{} //小的数据类型
class Fu{
String name;
public Fu(String name){
this.name = name;
}
public Animal eat() throws RuntimeException {
System.out.println(name+"吃番薯...");
return new Animal();
}
}
class Zi extends Fu{
String num;
public Zi(String name){
super(name);//指定调用父类带参的构造方法
}
//重写父类的eat方法
public Animal eat() throws Exception{
System.out.println("吃点开胃菜..");
System.out.println("喝点汤....");
System.out.println("吃点龙虾....");
System.out.println("吃青菜....");
System.out.println("喝两杯....");
System.out.println("吃点甜品....");
return new Animal();
}
}
class Demo10.4{
public static void main(String[] args){
Zi z = new Zi("大头儿子");
z.eat();
}
}
8、instanceof关键字
① 作用:判断一个对象是否属于指定的类别。
② 使用前提:判断的对象与指定的类别必须要存在继承或者实现的关系。
③ 使用格式:对象 instanceof 类别
④ 作用: 多态。一般我们做强制类型转换之前都会使用该关键字先判断一把,然后在进行转换的。
class Animal{
String name;
String color;
public Animal(String name, String color){
this.name = name;
this.color = color;
}
}
//狗是属于动物中一种
class Dog extends Animal {
public Dog(String name,String color){
super(name,color); //指定调用父类两个 参数的构造函数。
}
public void bite(){
System.out.println(name+"咬人!!");
}
}
//老鼠也是属于动物中一种
class Mouse extends Animal{
public Mouse(String name,String color){
super(name,color);
}
public void dig(){
System.out.println(name+"打洞..");
}
}
class Demo10.5{
public static void main(String[] args){
Dog d = new Dog("哈士奇","白色");
System.out.println("狗是狗类吗?"+(d instanceof Dog));//true
System.out.println("狗是动物类吗?"+(d instanceof Animal));//true
System.out.println("狗是老鼠类吗?"+ (d instanceof Mouse));=//报错
Animal a = new Animal("狗娃","黄色");
System.out.println("动物都是狗吗?"+ (a instanceof Dog));//false
}
}
9、final关键字
final关键字的用法:
① final关键字修饰一个基本类型的变量时,该变量不能重新赋值,第一次的值为最终的。
② fianl关键字修饰一个引用类型变量时,该变量不能重新指向新的对象。
③ final关键字修饰一个函数的时候,该函数不能被重写。
④ final关键字修饰一个类的时候,该类不能被继承。
常量的修饰符一般为: public static final
//圆形
class Circle{
double r; //半径
public static final double PI = 3.14; //固定不变的
public Circle(double r){
this.r = r;
}
//计算面积
public final void getArea(){
System.out.println("圆形的面积是:"+r*r*PI);
}
}
class Demo10.6 extends Circle{
public Demo10.6(double r){
super(r);
}
public static void main(String[] args)
{
final Circle c = new Circle(4.0);
test(c);
Demo10.6 c = new Demo10.6(4.0);
c.getArea();
}
public static void test(Circle c){
c = new Circle(5.0); //c变量又重新指向了新的对象。
c.getArea();
}
}
二、抽象类
1、抽象类的应用场景
我们在描述一类事物的时候,发现该种事物确实存在着某种行为,但是这种行为目前是不具体的,那么我们可以抽取这种行为的声明,但是不去实现该种行为,这时候这种行为我们称作为抽象的行为,我们就需要使用抽象类。
2、抽象类的好处: 强制要求子类一定要实现指定的方法。
3、抽象类要注意的细节:
① 如果一个函数没有方法体,那么该函数必须要使用abstract修饰,把该函数修饰成抽象的函数。
② 如果一个类出现了抽象的函数,那么该类也必须 使用abstract修饰。
③ 如果一个非抽象类继承了抽象类,那么必须要把抽象类的所有抽象方法全部实现。
④ 抽象类可以存在非抽象方法,也可以存在抽象的方法
⑤ 抽象类可以不存在抽象方法的。
⑥ 抽象类是不能创建对象的。
疑问:为什么抽象类不能创建对象呢?
答:因为抽象类是存在抽象方法的,如果能让抽象类创建对象的话,那么使用抽象的对象调用抽象方法是没有任何意义的。
⑦ 抽象类是存在构造函数的,其构造函数是提供给子类创建对象的时候初始化父类的属性的。
//动物类--->抽象类
abstract class Animal{
String name;
String color;
public Animal(String name,String color){
this.name = name;
this.color = color;
}
//非抽象的方法
public void eat(){
System.out.println(name+"吃粮食");
}
//移动...
public abstract void run();
}
//狗,是属于动物中一种
class Dog extends Animal{
public Dog(String name,String color){
super(name,color);
}
public void run(){
System.out.println(name+"四条腿跑得很快...");
}
}
//鱼,是属于动物中一种
class Fish extends Animal{
public Fish(String name,String color){
super(name,color);
}
public void run(){
System.out.println(name+"摇摇尾巴游啊游!");
}
}
4、abstract不能与以下关键字共同修饰一个方法
① abstract不能与private共同修饰一个方法
② abstract 不能与static共同修饰一个方法
③ abstract 不能与final共同修饰一个方法
三、值交换
1、值传递:调用一个方法的时候,传递给方法的参数 ,实际上传递变量所存储的值。
import java.util.*;
class Person{
int x =10;
}
class Demo10.7{
public static void main(String[] args){
int a = 3;
int b = 5;
changeValue(a,b);
System.out.println("交换之后的值:a = "+a +" b="+b); //值交换失败
int[] arr = {23,10,9};
changeArr(arr,1,2);
System.out.println("数的元素:"+ Arrays.toString(arr));//值交换成功
Person p = new Person();
changeObj(p,20);
System.out.println("x = "+ p.x);//20
}
public static void changeObj(Person p ,int x){
p.x = x;
}
//需求2: 定义一个函数交换数组中两个元素的位置。
public static void changeArr(int[] arr , int index1, int index2){
int temp = arr[index1];
arr[index1] = arr[index2];
arr[index2] = temp;
}
//需求1:定义一个函数交换两个基本类型变量的值。
public static void changeValue(int a , int b){
int temp = a;
a = b;
b = temp;
System.out.println("方法内部交换的值:a = "+a +" b="+b);
}
}