• 归档:类和对象


    1、使用类的静态字段和构造函数,我们可以跟踪某个类所创建对象的个数。请写一个类,在任何时候都可以向它查询“你已经创建了多少个对象?”

    程序设计思想:

    定义类的静态变量初始化为0.每创建一个对象此静态变量加一,来记录实例化对象的个数。

    源代码:

     1 package keTang_06;
     2 
     3 public class ClassCount {
     4 
     5     private static int count = 0; 
     6     
     7     public ClassCount(){
     8         count++;
     9     }
    10     
    11     public static int getCount(){
    12         return count;
    13     }
    14     
    15     
    16     public static void main(String[] args) {
    17         // TODO Auto-generated method stub
    18         ClassCount cc;
    19         for(int i = 0;i < 10;i++){
    20             cc = new ClassCount();
    21             System.out.println("已实例化了 " + cc.getCount() + " 个对象");
    22         }
    23     }
    24 
    25 }

    运行结果截图:

    2、

    如果类提供了自定义的构造方法,则系统不再提供默认的构造方法,实例化对象时调用默认的构造方法将编译出错

    3、初始化块、字段初始化、构造方法,执行顺序

    实验源代码:

     1 //探索初始化块、构造方法、字段默认值谁说了算
     2 
     3 package keTang_06;
     4 
     5 public class DuoChuShiHua {
     6 
     7     {
     8         field = 200;
     9         
    10     }
    11     public int field = 100;
    12     
    13     public DuoChuShiHua(int value){
    14         field = value;
    15     }
    16     
    17     public DuoChuShiHua(){
    18         
    19     }
    20     
    21     public static void main(String[] args) {
    22         // TODO Auto-generated method stub
    23         DuoChuShiHua duo1 = new DuoChuShiHua();
    24         System.out.println(duo1.field);
    25         
    26         DuoChuShiHua duo2 = new DuoChuShiHua(300);
    27         System.out.println(duo2.field);
    28     }
    29 
    30 }

    实验结果截图:

    通过第一个对象输出结果为100,说明初始化块先执行,然后字段的初始化后执行,所以最终结果为100

    通过第二个对象输出结果为300,说明最后执行的是构造方法。

    所以执行顺序为:

    初始化块->字段初始化->构造函数的初始化

    4、静态初始化块的执行顺序

    源代码:

     1 package keTang_06;
     2 class Root
     3 {
     4     static{
     5         System.out.println("Root的静态初始化块");
     6     }
     7     {
     8         System.out.println("Root的普通初始化块");
     9     }
    10     public Root()
    11     {
    12         System.out.println("Root的无参数的构造器");
    13     }
    14 }
    15 class Mid extends Root
    16 {
    17     static{
    18         System.out.println("Mid的静态初始化块");
    19     }
    20     {
    21         System.out.println("Mid的普通初始化块");
    22     }
    23     public Mid()
    24     {
    25         System.out.println("Mid的无参数的构造器");
    26     }
    27     public Mid(String msg)
    28     {
    29         //通过this调用同一类中重载的构造器
    30         this();
    31         System.out.println("Mid的带参数构造器,其参数值:" + msg);
    32     }
    33 }
    34 class Leaf extends Mid
    35 {
    36     static{
    37         System.out.println("Leaf的静态初始化块");
    38     }
    39     {
    40         System.out.println("Leaf的普通初始化块");
    41     }    
    42     public Leaf()
    43     {
    44         //通过super调用父类中有一个字符串参数的构造器
    45         super("Java初始化顺序演示");
    46         System.out.println("执行Leaf的构造器");
    47     }
    48 
    49 }
    50 
    51 public class TestStaticInitializeBlock
    52 {
    53     public static void main(String[] args) 
    54     {
    55         new Leaf();
    56         
    57 
    58     }
    59 }

    运行结果截图:

    说明执行顺序为:基类的静态初始化块 > 子类的静态初始化块 > 基类的普通初始化块 > 基类的构造方法 > 子类的普通初始化块 > 子类的构造方法

    静态初始化块只执行一次!!!

    5、在静态方法中访问类的实例成员

    静态方法一般只能访问静态变量,如果要访问类的实例变量,那么需要先实例化对象

    实验源代码:

     1 package keTang_06;
     2 
     3 public class StaticMethod {
     4 
     5     public static int a = 1;
     6     public int b = 2;
     7     
     8     public void func(){
     9         System.out.println("我是一个实例方法。");
    10     }
    11     
    12     
    13     public static void staticFunc(){
    14         System.out.println("我是一个静态方法!!!");
    15         System.out.println("静态变量 a = " + a);
    16         System.out.println("实例变量 b = " + new StaticMethod().b);
    17         new StaticMethod().func();
    18     }
    19     
    20     public static void main(String[] args) {
    21         // TODO Auto-generated method stub
    22         new StaticMethod().staticFunc();
    23     }
    24 
    25 }

    运行结果截图:

  • 相关阅读:
    在线漏洞检测网站
    渗透测试工具库
    端口利用解析
    Linux安全脚本
    Linux常见系统故障
    Oracle
    Redis和MongoDB区别
    MHA在监控和故障转移时都做了什么
    Oracle 11g Dataguard参数详解
    Oracle
  • 原文地址:https://www.cnblogs.com/liuxining/p/6003855.html
Copyright © 2020-2023  润新知