面向对象(11)(FIANL和抽象类)

final class demo 这个类被继承 运行就报错了

 

final后的函数被复写会报错

运行错误

 

所有常量名全部大写 多个单词 下划线 隔开

 

权限够大 就算静态 也其意义不变

 注:当执行静态初始化的时候可以对类属性进行赋初始值,当执行普通初始化,构造器的时候可以对实例属性进行赋初始值

final 修饰成员变量的效果

1.final修饰实例属性,要么在定义该属性时指定初始值,要么在普通初始化块,或者构造器中为该属性赋值,如果普通初始化被某个实例已经赋值过了再次赋值是错误的。

2.修饰类属性的时候,要么在定义该属性时指定初始值,要么在静态初始化块,实例时属性不能在静态初始化块赋初始值,因为静态不能访问非静态  类属性不能在普通初始化赋值,因为已经赋值过了 再次赋值是错误的。

 

  final int a =6;
   final String str; //在下面初始化块中赋值
   final static  double d;//在下面初静态始化块中赋值
   {
    str = "d";//为str赋值
  //  a = 9; //错误 不能再次赋值
   }
  
   static
   {
    d =1;
   }
  
   final int s;//再下面构造器中赋值
   public TestInherit()
   {
  s =9;
  }
  //  final int cc;
  // public void ss(){
   //  cc = 9; //错 。不能用普通方法赋值
 //  }
  
 //final char ch; //错。没有默认值也没在初始化块中赋值

 

2.final修饰局部变量

修饰局部变量时在定义的时候可以没值,但必须在后面的代码中将值加上去 但只能赋值一次 不能再重新赋值

当然在定义的时候赋值后面就不能赋值了

3.final 修饰基本类型和引用类型的区别

修饰基本类型 只能赋值一次

修饰引用类型 不可能改变引用地址 可以重新为地址里面的变量赋值


import java.util.Arrays;

public class TestInherit {
 
  private int age;
  public TestInherit() {
  
 }
 public TestInherit(int age){
  this.age = age;
 }
 public int getAge() {
  return age;
 }
 public void setAge(int age) {
  this.age = age;
 }
 
 public static void main(String[] args) {
  //fianl 修饰数组变量,iArr是一个引用
  final int[] iArr = {5,6,12,9};
  System.out.println(Arrays.toString(iArr));//打印元素值
  Arrays.sort(iArr); //对数组元素排序
  System.out.println(Arrays.toString(iArr));//排位后打印
  iArr[2] = -3;//合法
  System.out.println(Arrays.toString(iArr));
  //iArr = null;//重新赋值错误
  
  final TestInherit t = new TestInherit();
  t.setAge(12); //赋值没错
  System.out.println(t.getAge());
  //t = null; //改变地址错误   
 }
}

 

final修饰方法

final修饰的方法不可以重写但是可以重载

final修饰类不可以被继承了

 

 

 

 

 

 

{
  System.out.println("执行Shape的初始化块。。。。");
 }
 private String color;
 
 public abstract double calPerimtter();
 
 public abstract String getType();//定义一个返回形状抽象的方法
 public Shape(){
  
 }
 
 public Shape(String color){
  System.out.println("执行Shape的构造器。。");
  this.color = color;
 }

 public String getColor() {
  return color;
 }

 public void setColor(String color) {
  this.color = color;
 }
 

public class Triangle extends Shape{
     private double a;
     private double b;
     private double c;
 
   
  public  Triangle(String color, double a, double b, double c) {
  super(color);
 
 }

    public void setSides(double a, double b,double c){
       if(a+b >= c || a>= b+c || b >=a+c )
       {
        System.out.println("三角形2边之和必须大于或者等于第3边");
       }
       this.a = a;
       this.b =b;
       this.c =c;
    
    }

 public double calPerimtter() {
  return a + b + c;
 }

 
 public String getType() {
 
  return "三角形";
 }

public class Circle extends Shape{

 
  private double radius;
 
  public Circle(String color ,double radius){
  
   super();
   this.radius = radius;
  }
 
  public void setRadius(double radius)
  {
   this.radius = radius;
  }
 
   //重写Shape类的计算周长的抽象方法
 public double calPerimtter() {
 
  return 2 * Math.PI * this.radius;
 }

    // 重写Shape类返回形状的抽象的方法
 public String getType() {
 
  return getColor() + "圆形";
 }
  
 public static void main(String[] args) {
   Shape s1 = new Triangle("黑色",3,4,5);
   Shape s2 = new Circle("黄色", 3);
   System.out.println(s1.getType());
   System.out.println(s1.calPerimtter());
   System.out.println(s2.getType());
   System.out.println(s2.calPerimtter());
 }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值