zoukankan      html  css  js  c++  java
  • java===java基础学习(13)---this,static(静态变量和静态方法)的使用

    package dog;
    
    public class PersonAndDog {
        
        public static void main(String[] args)
        {    
            Dogs da_huang = new Dogs(12,"da_huang");
         
         //共同引用da_huang这条狗 person p1
    = new person(12,"李明",da_huang); person p2 = new person(13,"jack",da_huang); p1.get_info(); p2.get_info();    } } class Dogs { private int age; private String name; public Dogs(int age ,String name) { this.age = age; this.name = name; } public String get_name() { System.out.println("The dog is named"+this.name); return this.name; } } class person { int age; String name; Dogs dog; public person(int age ,String name, Dogs dog) { this.age = age; this.name = name; this.dog = dog; } public void get_info() { System.out.println("我的名字叫"+this.name+",我今年"+this.age+",岁。我的狗叫"+this.dog.get_name()); } }

     

    this主要用来区分不同的对象,他们的成员属性的。

    this属于一个对象,不属于一个类。

     

     

    区分成员变量和局部变量

    代表当前对象

    构造器与构造器之间的调用

     

    static的使用

    静态变量是该类的所有对象共享的变量,任何一个该类的对象去访问它时,取到的都是相同的值,同样任何一个该类的对象去修改它时,修改的也是同一个变量。

     

    定义语法:
    访问修饰符 static 数据类型 变量名;

     

    如何访问类变量
    类名.静态变量名 或者 对象名.静态变量名

    package dog;
    
    public class Static {
        public static void main(String[] args)
        {
            Child c1 = new Child("竹子");
            c1.add();
            Child c2 = new Child("花花");
            c2.add();
            Child c3 = new Child("草草");
            c3.add();
            
            //count 是静态变量,因此可以被任何一个对象访问
            System.out.println("孩子人数为="+c3.count);
        }
    
    }
    
    
    class Child
    {
        String name;
        static int count; //count 是静态变量,因此可以被任何一个对象访问
        
        public Child(String name)
        {
            this.name = name;
        }
        
        public void add()
        {
            System.out.println(this.name+"加入到游戏中了!");
            count++;
        }
    }

     

     

    public class Demo3_2 {
        static int i =1;
        static
        {
            //该静态区域块,只被执行一次
            System.out.println("静态代码区");
            i ++;
        }
        
        public Demo3_2()
        {    
            System.out.println("构造函数");
            i++;
        }
        public static void main(String []args)
        {
            Demo3_2 t1 = new Demo3_2();
            System.out.println(t1.i);           //3
            
            Demo3_2 t2 = new Demo3_2();
            System.out.println(t2.i);            //4
            System.out.println(Demo3_2.i);
        }
        
    }
    //注意一点,即使不创建新的对象。静态代码区域块的也会被执行,且执行一次

     

     

    package dog;
    
    public class Demo3_3 {
        public static void main(String []args)
        {
            Stu s1= new Stu(12,"王二",4550);
            
            Stu s2 = new Stu(13, "len",5000);
            System.out.println(Stu.get_count());  //这里直接使用的是类名.类方法名的形式
        }
    
    }
    
    class Stu
    {
        int age;
        String name;
        int xuefei;
        static int count;
        
        public Stu(int age, String name, int xuefei)
        {
            this.age = age;
            this.name=name;
            this.xuefei=xuefei;
            count += xuefei;
        }
        
        public static  int get_count()
        {
            //静态方法(类方法),是属于对象实例的,形式如下:
            //访问修饰符 static 数据返回类型 方法名(){}
            //注意: 静态方法(类方法)中不能访问非静态变量(类变量)
            //使用: 类名.静态方法名  或者 对象名.静态方法名
            return count;
        }
    }

     

    静态变量

    加上static称为类变量或静态变量,否则称为实例变量

    静态变量是与类相关的,公共的属性

    实例变量属于每个对象个体的属性

    静态变量可以通过    类名.静态变量名   直接访问

    静态方法
    静态方法属于与类相关的,公共的方法。

    实例方法属于每个对象个体的方法。

    静态方法可以通过   类名.静态方法名 直接访问

  • 相关阅读:
    Codeforces Round #251 (Div. 2) A
    topcoder SRM 623 DIV2 CatAndRat
    topcoder SRM 623 DIV2 CatchTheBeatEasy
    topcoder SRM 622 DIV2 FibonacciDiv2
    topcoder SRM 622 DIV2 BoxesDiv2
    Leetcode Linked List Cycle II
    leetcode Linked List Cycle
    Leetcode Search Insert Position
    关于vim插件
    Codeforces Round #248 (Div. 2) B. Kuriyama Mirai's Stones
  • 原文地址:https://www.cnblogs.com/botoo/p/8777061.html
Copyright © 2011-2022 走看看