Final关键字——
package Test;
public class TestFinal {
//实例变量
final int a=10; //a的值不能被修改
public int c=20;
public static int g=29;
//类变量
public static final int b=20; //在类方法中b的值也不能被修改
public void method1(){
//a=10;
}
public static void method2(){
// b=29;
}
//1、java中final可以修饰局部变量
public static void method3(){
//final修饰内置类型的变量
final int a=10; //经过fianl关键字修饰变量之后就会变成常量
System.out.println(a);
// a=100;此时a的值不能被改变
System.out.println(a);
//final修饰引用类型的变量
final String name="小八";
// name="小七";
//表明array的指向不能被修改,数组中的值可以被修改
final int[] array={1,2,3,4,5,6};
array[2]=2;
// array=new int[]{1,3,4,5,6};此时编译报错,final为最终赋值想要修改数组指向不行
}
//不管final修饰类方法还是实例方法都不能对final修饰的变量进行改变
public void method5(){
// a=1;
g=30;
}
public static void method6(){
// a=100;
g=38;
}
//final修饰静态成员方法
public static final void method7(){
//静态方法中只能访问类变量,不能访问实例变量
//因为实例变量保存在队象中,只能通过this访问,而静态方法中没有this引用
g=39;
}
//注意:final不能修饰构造方法
public static void main(String[] args) {
}
}
package Test;
public final class FinalClass {
int a;
int b;
public FinalClass(int a, int b) {
this.a = a;
this.b = b;
}
}
package Test;
//经过final修饰的类是无法被其他类所继承的=
//public class project extends FinalClass{
public class project {
public static void main(String[] args) {
}
}
Private修饰类也可以使其不能被子类继承但可以使用方法调用但是比较麻烦
package Test;
public class Base {
int a;
int b;
private Base(int a, int b) {
this.a = a;
this.b = b;
}
public static Base getInstance(int a,int b){
return new Base(a,b);
}
}
package Test;
public class Derive{
public static void main(String[] args) {
Base b=Base.getInstance(1,3);
}
}
继承表示对象之间是is-a的关系,比如:狗是动物,猫是动物
组合表示对象之间是has-a的关系,比如:汽车
package Test.One;
public class Car {
//Tire 、Engine、VehicleSystem和car之间的关系就是组合
protected Tire t;
protected Engine e;
protected VehicleSystem vs;
public Car(Tire t, Engine e, VehicleSystem vs) {
this.t = t;
this.e = e;
this.vs = vs;
}
public void drive(){
}
public void music(){
vs.music();
}
public void navigate(){
vs.navigate();
}
}
package Test.One;
public class Engine {
public String brand;
public Engine(String brand) {
this.brand = brand;
}
}
package Test.One;
public class Tire {
private String brand;
private int Radius;
public Tire(String brand, int radius) {
this.brand = brand;
Radius = radius;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public int getRadius() {
return Radius;
}
public void setRadius(int radius) {
Radius = radius;
}
}
package Test.One;
public class VehicleSystem {
public String osname;
public VehicleSystem(String osname) {
this.osname = osname;
}
public void music(){
System.out.println("听音乐");
}
public void navigate(){
System.out.println("导航");
}
}
package Test.One;
public class BMW extends Car{
BMW(Tire t,Engine e,VehicleSystem vs){
super(t,e,vs);
}
public static void main(String[] args) {
Tire t=new Tire("米其林",67);
VehicleSystem vs=new VehicleSystem("东风");
Engine e=new Engine("鸿蒙");
BMW b=new BMW(t,e,vs);
b.drive();
b.music();
b.navigate();
}
}
多态的概念:通俗来说,就是多种形态,具体点就是去完成某个行为,当不同的对象去完成时会产生出不同 的状态。
1. 必须在继承体系下
2. 子类必须要对父类中方法进行重写
3. 通过父类的引用调用重写的方法
多态体现:在代码运行时,当传递不同类对象时,会调用对应类中的方法。
package Test.Two;
public class Animal {
protected String name;
protected String gender;
protected int age;
public Animal(String name, String gender, int age) {
this.name = name;
this.gender = gender;
this.age = age;
}
public void sleep(){
System.out.println(name+"在打呼儿~~~");
}
public void eat(){
System.out.println(name+"卖命地在干饭!!!");
}
public void bark(){
System.out.println(name+"在狗叫~~~");
}
}
package Test.Two;
public class Cat extends Animal{
protected String temper;
public Cat(String name, String gender, int age, String temper) {
super(name, gender, age);
this.temper = temper;
}
//重写了Animal中的sleep方法
public void sleep(){
System.out.println(name+"zzz~~~");
}
//重写了Animal中的eat方法
public void eat(){
System.out.println(name+"在干饭!!!");
}
//重写了Animal中的bark方法
public void bark(){
System.out.println(name+"喵喵喵~~~");
}
}
public class Dog extends Animal{
protected String color;
public Dog(String name, String gender, int age, String color) {
super(name, gender, age);
this.color = color;
}
//重写了Animal中的sleep方法
public void sleep(){
System.out.println(name+"在睡觉");
}
//重写了Animal中的eat方法
public void eat(){
System.out.println(name+"在干饭");
}
//重写了Animal中的bark方法
public void bark(){
System.out.println(name+"在叫");
}
}
package Test.Two;
public class TestAnimal {
public static void method(Animal animal){
animal.bark();
animal.eat();
animal.sleep();
}
public static void main(String[] args) {
System.out.println();
Dog d1=new Dog("小八","公",3,"黄色");
method(d1);
System.out.println("-----------");
Cat c1=new Cat("老八","母",2,"粘人型");
method(c1);
}
}
子类在重写父类的方法时,一般必须与父类方法原型一致: 返回值类型 方法名 (参数列表) 要完全一致
被重写的方法返回值类型可以不同,但是必须是具有父子关系的
访问权限不能比父类中被重写的方法的访问权限更低。例如:如果父类方法被public修饰,则子类中重写该方法就不能声明为 protected
父类被static、private修饰的方法、构造方法都不能被重写。
重写的方法, 可以使用 @Override 注解来显式指定. 有了这个注解能帮我们进行一些合法性校验. 例如不小心将方法名字拼写错了 (比如写成 aet), 那么此时编译器就会发现父类中没有 aet 方法, 就会编译报错, 提示无法构成重写
package Test.Three;
public class Ticket {
public String name;
public String gender;
public int age;
public Ticket(String name, String gender, int age) {
this.name = name;
this.gender = gender;
this.age = age;
}
public void BuyTicket(){
System.out.println("买全价票");
}
}
package Test.Three;
public class Children extends Ticket{
public int identity;
public Children(String name, String gender, int age, int identity) {
super(name, gender, age);
this.identity = identity;
}
public void BuyTicket(){
System.out.println(name+"买半价票");
};
}
package Test.Three;
public class Person extends Ticket {
public String occupcation;
public Person(String name, String gender, int age, String occupcation) {
super(name, gender, age);
this.occupcation = occupcation;
}
public void BuyTicket(){
System.out.println(name+"买全价票");
}
}
package Test.Three;
public class TestTicket {
public static void method(Person p){
p.BuyTicket();
}
public static void main(String[] args) {
Person p1=new Person("晓红","女",45,"工人");
p1.BuyTicket();
System.out.println("-----------");
Children c1=new Children("小红","女",7,1234567891);
c1.BuyTicket();
}
}
重写的方法和不能被重写的方法
本文含有隐藏内容,请 开通VIP 后查看