zoukankan      html  css  js  c++  java
  • [JAVA · 0基础]:11.终结者-final

    关于finalkeyword的总结,是非常easy学习和掌握的,大致分为用final修饰的符号常量、类、方法、属性四部分。而这四部分的内容也是我们必须掌握的,非常有用。在项目中常常来利用finalkeyword来帮忙以完毕我们特定的任务。以下就来看看终于的final:

    符号常量

        用 final 修饰的变量就称之为终于变量。也称之为符号常量。

    比如:

     double PI = 3.14;

    ===>>>  final double PI = 3.14;  //符号常量

    Demo

    <span style="font-size:18px;"><span style="font-size:18px;">package com.hqg.oop.d37.a3;
    public class finalTest {
    	public static void main(String[] args) {
    		final double PI = 3.14;
    		//PI = 3.1415926;  //符号常量不可中途改变其值。
    		SonDemo1  sd = new SonDemo1();
    		int re = sd.add(10, 20);
    		System.out.println( re );
    	}
    }
    class Person{
    	private String name;
    	private final boolean sex   ; //终于属性(仅仅读属性)
    	
    	public Person( String name, boolean sex ) {
    		this.name = name;
    		this.sex = sex;
    	}
    }
    class DemoA {
    	public final int add( int x, int y ){
    		return x + y;
    	}
    }
    class SonDemo1 extends DemoA {
    }
    //class Son extends  String {
    //	
    //}
    final class Father {	
    }
    //class Son extends Father {
    //	
    //}</span></span>

    用 final 修饰的类,即为终于类。

    特性:

    它不可有子类。即: 防继承的。

    比如: 专家提供的 String 类就量个终于类。

    方法

    用 final 修饰的方法。即为终于方法。

    特性:

    它不可被重写。即: 防重写。

    属性

    用final修饰的实例变量就是终于属性,也成为仅仅读属性。

    特性:

      1) 它必须由final修饰。

    2) 它一旦赋值。则终身不变。(中途不变)

      3) 当声明终于的实例变量时。若没有赋值,则在每个构造器中必须对它赋值。

      4) 终于的实例变量没有设定器。

      5) 终于的实例变量名称大写。

    Demo 

    <span style="font-size:18px;"><span style="font-size:18px;">package com.hqg.oop.d38.a1;
    
    public class FinalObjectVarTest {
    	public static void main(String[] args) {
    		Person p1 = new Person("张灵",false, 18);
    		Person p2 = new Person("张灵",false, 18);
    		System.out.println( p1 );
    		System.out.println( p2 );
    		
    		System.out.println( p1.equals(p2) );
    		
    		//p1.SEX = true;
    	}
    }
    
    class Person {
    	//实例变量
    	private String name;
    	public  final boolean SEX; //约定: true为男, false为女
    	private int age;
    	
    	public Person() {
    		int n = (int)(2 * Math.random());
    		this.SEX = n == 1 ;
    	}
    	public Person(String name,int age) {
    		this(); 
    		this.name = name;
    		this.age = age;
    	}
    	public Person(String name, boolean sex, int age) {
    		this.name = name;
    		SEX = sex;
    		this.age = age;
    	}
    	
    	public void setName(String name) {
    		this.name = name;
    	}
    	public void setAge(int age) {
    		this.age = age;
    	}
    	
    	//訪问器
    	public String getName() {
    		return name;
    	}
    	public boolean isSEX() {
    		return SEX;
    	}
    	public int getAge() {
    		return age;
    	}
    	
    	//
    	@Override
    	public String toString() {
    		return "姓名: " + name + " 性别: " + (SEX ? "男":"女") + "  年龄: " + age;
    	}
    	
    	@Override
    	public boolean equals(Object obj) {
    		Person pp = (Person)obj;
    		if( this.name.equals( pp.name ) && this.SEX == pp.SEX && this.age == pp.age ){
    			return true;
    		}else{
    			return false;
    		}
    	}
    }
    </span></span>

    业务思想

    就像英语中的一句谚语:”Each coin has two sides”(每一个硬币都有两面),keywordfinal正如那仅仅硬币。用final来定义符号常量、类、方法、属性能够降低我们非常多工作,容错率也提高了非常多。


  • 相关阅读:
    CodeForces 659F Polycarp and Hay
    CodeForces 713C Sonya and Problem Wihtout a Legend
    CodeForces 712D Memory and Scores
    CodeForces 689E Mike and Geometry Problem
    CodeForces 675D Tree Construction
    CodeForces 671A Recycling Bottles
    CodeForces 667C Reberland Linguistics
    CodeForces 672D Robin Hood
    CodeForces 675E Trains and Statistic
    CodeForces 676D Theseus and labyrinth
  • 原文地址:https://www.cnblogs.com/gavanwanggw/p/7258903.html
Copyright © 2011-2022 走看看