zoukankan      html  css  js  c++  java
  • java学习中,instanceof 关键字 和 final 关键字、值的传递(java 学习中的小记录)

    java学习中,instanceof 关键字 和 final 关键字、值的传递(java 学习中的小记录)作者:王可利(Star·星星)

    instanceof 关键字

    作用:

        1.用来判断某个对象是否属于某一个类。

        2.instanceof 关键字使用的前提:对象指定的类有继承关系或者实现关系

        3.强制类型转换会用到 instanceof 关键字。

          如:

          Student s = (Student)new Person();//要想这么做,必须满足Student继承Person。

          boolean res = s instanceof Person 

          if( res){

          Student s = (Student)new Person();

        }

    代码如下:

     1 package study;
     2 
     3 class Person{
     4     String name;
     5     public Person(String name) {
     6         this.name  = name;
     7     }
     8     public Person() {}
     9 }
    10 
    11 class Student extends Person{
    12     
    13 }
    14 
    15 public class star {
    16     public static void main(String[] args) {
    17         Person star = new Person("星星");        
    18         boolean result1 = star instanceof Person;
    19         System.out.println(result1);//ture
    20         
    21         Student s = new Student();//默认父类的无参构造
    22         
    23         boolean result2 = s instanceof Person;
    24         System.out.println(result2);//ture
    25     }
    26 
    27 }

    final 关键字(修饰符,最终的)

    1.如果用一个final 关键字修饰一个基本数据类型变量,改变了就不能重新赋值。第一次的结果为最终结果。

    2.如果用final修饰引用数据类型,引用数据类型的变量就不能再赋值。

       如:这样是错的。

             final Yuan yuan = new Yuan(10);

             yuan = new Yuan(100);//无法重新赋值

    3.如果final修饰一个方法,方法就不能被重写。

    4.如果final修饰一个类,那么这个类就不能被继承。

    5.final 修饰的变量必须要初始化,不然会报错。

     1 package study;
     2 
     3 class Yuan{
     4     int r;
     5     final double pai = 3.14;//应该是一个不变的量,不然下面参数 给了 不是3.14 就会被修改。类似常量
     6     
     7     public Yuan(int r/*,double pai*/) {
     8         this.r = r;
     9     /*    this.pai = pai;*/
    10     }
    11     
    12     public void area(){
    13         System.out.println("圆的面积是"+r*r*pai);
    14     }
    15 }
    16 
    17 public class star {
    18     public static void main(String[] args) {
    19         Yuan yuan = new Yuan(10/*,3.14*/);//不给修饰会被修改
    20         yuan.area();
    21     }
    22 }

    如何用final表示常量:

    格式:

        public  final  static 基本数据类型  变量名

    值的传递

    基本数据类型之间赋值,实际是将变量的值赋给另外一个变量。

    参数如果是引用类型,传递参数是参数的引用类型数据的地址。

    引用类型:类、接口、数组

     1 package study;
     2 
     3 public class star {
     4     public static void main(String[] args) {
     5         
     6         //需求:定义两个整形的数,用一个方法来交换两个数
     7         int a = 10;
     8         int b = 20;        
     9         change(a, b);
    10         System.out.println("a = "+a+",b = "+b);//10.20
    11         
    12         //需求:定义一个数组,交换两个数的位置
    13         int[] arr = {10,20};
    14         changeArr(arr);//20,10
    15         System.out.println("arr[0] = "+arr[0]+"arr[1] = "+arr[1]);        
    16     }
    17     
    18     public static void change(int a,int b){
    19         int temp = a;
    20         a = b;
    21         b = temp;
    22     }
    23     public  static void changeArr(int[] arr){
    24         int temp = arr[0];
    25         arr[1] = arr[0];
    26         arr[0] = temp;
    27     }
    28 }
  • 相关阅读:
    Mac php使用gd库出错 Call to undefined function imagettftext()
    centos 使用 locate
    Mac HomeBrew 安装 mysql
    zsh 命令提示符 PROMPT
    新的开始
    Java 面试题分析
    Java NIO Show All Files
    正确使用 Volatile 变量
    面试题整理 2017
    有10阶梯, 每次走1,2 or 3 阶,有多少种方式???
  • 原文地址:https://www.cnblogs.com/StarKL/p/6131408.html
Copyright © 2011-2022 走看看