2024/10/14日 动手动脑

1. 关于继承中成员变量的访问特点

代码示例:

点击查看代码
public class Main {
    public static void main(String[] args) {
        Zi z=new Zi();
        z.ziShow();
    }
}

class Fu{
    String name = "Fu";
}
class Zi extends Fu{
    String name = "Zi";
    public void ziShow(){
        String name = "ZiShow";
        System.out.println(name);
        System.out.println(this.name);
        System.out.println(super.name);
    }
}
![](https://img2024.cnblogs.com/blog/3478956/202410/3478956-20241014211847948-373916367.png) 根据测试结果,我们可知,在无修饰词时,在范围内我们是就近原则。 此外,this指当前类,super指其父类。

2.动手实验 “==”
/1/

点击查看代码
public class Main {
    public static void main(String[] args) {
        int value1 = 100;
        int value2 = 100;
        System.out.println(value1==value2); 
    }
}
//------------>ture

/2/

点击查看代码
public class Main {
    public static void main(String[] args) {
        Foo obj1=new Foo();
        Foo obj2=new Foo();
        System.out.println(obj1==obj2);
    }
}
class Foo{
    int value = 100;
}
//------------>false;
总结: 当“==”施加于原始数据类型变量时,是比较变量所保存的数据是否相等 当“==”施加于引用类型变量时,是比较这两个变量是否引用同一对象。 引用代表地址,所以“==”实际上相当于比较两个引用类型变量中保存的对象地址是否相同。 **3.构造方法与类** /1/
点击查看代码
public class Main {
    public static void main(String[] args) {
        Foo obj1=new Foo();
    }
}
class Foo{
    int value ;
    public Foo(int initValue){
        value=initValue;
    }
}
//如果类提供了一个自定义的构造方法,将导致系统不再提供默认构造方法。
/2/
点击查看代码
class Fruit {
    int grams;
    int calsPerGram;
    Fruit() {
        this(55,10);
    }
    Fruit(int g,int c){
        grams=g;
        calsPerGram=c;
    }
}
//同一个类可以有多个构造函数,多个构造函数之间通过参数来区分。
// 这是方法重载的一个实例。构造函数之间可以相互调用。
/3/自找麻烦
点击查看代码
public class Main {
    public static void main(String[] args) {
        Foo obj=new Foo();
        System.out.println(obj.field);
        obj = new Foo(300);
        System.out.println(obj.field);
    }
}

class Foo{
    {
        field =200;
    }
    public int field = 100;
    public Foo(int value){
        this.field = value;
    }
    public Foo(){
        System.out.println(field);
    }
}
//------------>100;
//------------>100;
//------------>300;
//这是一个生造出来展示Java语法特性的示例类,在实际开发中不要这样写代码,应该尽量保证一个字段只初始化一次!
//1.执行类成员定义时指定的默认值或类的初始化块,到底执行哪一个要看哪一个“排在前面”。
//2.执行类的构造函数。
//类的初始化块不接收任何的参数,而且只要一创建类的对象,它们就会被执行。
//因此,适合于封装那些“对象创建时必须执行的代码”。

/4/类的静态初始化块

点击查看代码
public class Main {
    public static void main(String[] args) {
        new Leaf();
    }
}

class Root
{
    static
    {
        System.out.println("Root的静态初始化块");
    }
    {
        System.out.println("Root的普通初始化块");
    }
    public Root()
    {
        System.out.println("Root的无参数的构造器");
    }
}
class Mid extends Root
{
    static
    {
        System.out.println("Mid的静态初始化块");
    }
    {
        System.out.println("Mid的普通初始化块");
    }
    public Mid()
    {
        System.out.println("Mid的无参数的构造器");
    }
    public Mid(String msg)
    {
        //通过this调用同一类中重载的构造器
        this();
        System.out.println("Mid的带参数构造器,其参数值:" + msg);
    }
}
class Leaf extends Mid
{
    static
    {
        System.out.println("Leaf的静态初始化块");
    }
    {
        System.out.println("Leaf的普通初始化块");
    }
    public Leaf()
    {
        //通过super调用父类中有一个字符串参数的构造器
        super("Java初始化顺序演示");
        System.out.println("执行Leaf的构造器");
    }

}

//Root的静态初始化块
//Mid的静态初始化块
//Leaf的静态初始化块
//Root的普通初始化块
//Root的无参数的构造器
//Mid的普通初始化块
//Mid的无参数的构造器
//Mid的带参数构造器,其参数值:Java初始化顺序演示
//Leaf的普通初始化块
//执行Leaf的构造器

//由测试结果可知,其运行过程为最高父类静态,父类静态,子类静态,最高父类普通和构造函数,父类普通和构造函数,子类普通和构造函数

/5/有趣的问题
问: 静态方法中只允许访问静态数据,那么,如何在静态方法中访问类的实例成员(即没有附加static关键字的字段或方法)?
请编写代码验证你的想法。
已知:在静态方法中不能直接访问类的实例成员。但是可以通过创建类的实例来间接访问实例成员。因此如下:

点击查看代码
class MyClass {
    int instanceVariable;

    public MyClass(int instanceVariable) {
        this.instanceVariable = instanceVariable;
    }

    public void instanceMethod() {
        System.out.println("This is an instance method. Instance variable value: " + instanceVariable);
    }

    public static void staticMethod() {
        MyClass obj = new MyClass(42);
        System.out.println("Accessing instance variable through an object: " + obj.instanceVariable);
        obj.instanceMethod();
    }
}

public class Main {
    public static void main(String[] args) {
        MyClass.staticMethod();
    }
}

//在这个例子中,静态方法staticMethod通过创建MyClass的实例来访问实例成员变量instanceVariable和实例方法instanceMethod。
**4.包装类**
点击查看代码
public class Main {
    public static void main(String[] args) {
        Integer i1 = 100;
        Integer j1 = 100;
        System.out.println(i1==j1);

        Integer i2 = 129;
        Integer j2 = 129;
        System.out.println(i2==j2);

    }
}
-------->true
-------->false
//在 Java 中,对于Integer类型的自动装箱和拆箱操作,在一定范围内的值会被缓存起来以提高性能。
//对于Integer类型,范围在-128到127之间的值会被缓存,当使用自动装箱(如Integer i1 = 100)时,如果值在这个范围内,会从缓存中获取相同的Integer对象。
//所以当比较两个在这个范围内的Integer对象使用==时,实际上比较的是同一个对象的引用,结果为true。
//而对于不在这个范围内的值,比如129,每次自动装箱都会创建新的Integer对象,所以当使用==比较两个不同的Integer对象时,结果为false。
//如果要比较Integer对象的值是否相等,应该使用equals方法。
                                                                                                    --------------------------Moonbeams.
posted @   Moonbeamsc  阅读(5)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· DeepSeek在M芯片Mac上本地化部署
返回顶端
点击右上角即可分享
微信分享提示