Java教程

Java default关键字

Java default关键字是访问修饰符。如果您没有为变量,方法,构造函数和类分配任何访问修饰符,默认情况下,它将被视为默认访问修饰符。

要记住的要点

default访问修饰符只能在程序包中访问。 与private和protected不同,我们可以通过不向其分配任何访问修饰符来创建默认的外部类。在这种情况下,不限于采用类似于程序名称的类名称。 如果您要覆盖任何方法,则被覆盖的方法(即在子类中声明的方法)必须没有更多限制。因此,不允许默认方法或变量使用私有访问修饰符。

default关键字示例

示例1

让我们看一个示例,以确定default变量是否可访问或不在包装之外。
package com.java;
public class A {
    String msg="try to access default variable outside the package";
}
//save by DefaultExample1.java
package com.lidihuo;
import com.java.A;
public class DefaultExample1 {
    public static void main(String[] args) {
        A a=new A();
        System.out.println(a.msg);
    }
}
输出:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The field A.msg is not visible

示例2

让我们看一个示例,以确定是否可以在包内的类外部访问default变量。
class A {
    String msg="try to access the default variable outside the class within the package";
}
public class DefaultExample2 {
    public static void main(String[] args) {
        A a=new A();
        System.out.println(a.msg);
    }
}
输出:
Try to access the default variable outside the class within the package

示例3

让我们看一个示例,以确定在程序包外部是否可以访问默认方法。
//save by A.java
package com.java;
public class A {
    void msg()
 {
        System.out.println("try to access default method outside the package ");
    }
}
//save by DefaultExample2.java
package com.lidihuo;
import com.java.A;
public class DefaultExample2 {
    public static void main(String[] args) {
        A a=new A();
        a.msg();
    }
}
输出:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method msg() from the type A is not visible

示例4

让我们看一个示例,该示例确定默认方法是否可以使用继承在程序包外部访问。
//save by A.java
package com.java;
public class A {
    void msg()
 {
        System.out.println("try to access default method outside the package using inheritance");
    }
}
//save by DefaultExample4.java
package com.lidihuo;
import com.java.A;
public class DefaultExample4 extends A {
    public static void main(String[] args) {
        DefaultExample4 a=new DefaultExample4();
        a.msg();
    }
}
输出:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method msg() from the type A is not visible

示例5

让我们看一个确定我们是否使用默认外部类的示例。
class DefaultExample5 {
    void display() {
        System.out.println("try to access outer default class");
    }
    public static void main(String[] args) {
        DefaultExample5 p=new DefaultExample5();
        p.display();
    }
}
输出:
Try to access outer default classs

示例6

让我们看一个示例,以确定我们是否从类外部创建默认构造函数的实例。
package com.java;
public class A {
    String msg;
    A(String msg) {
        this.msg=msg;
    }
    public void display() {
        System.out.println(msg);
    }
}
//save by DefaultExample6.java
package com.lidihuo;
import com.java.A;
public class DefaultExample6 {
    public static void main(String[] args) {
        A a=new A("try to create the instance of default constructor outside the package");
        a.display();
    }
}
输出:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The constructor A(String) is not visible

示例7

让我们看一个示例,以确定是否使用默认访问修饰符将默认方法重写为子类。
class A {
    void msg() {
        System.out.println("try it");
    }
}
//save by DefaultExample7.java
class DefaultExample1 extends A {
    void msg() {
        System.out.println("try to access the overridden method");
    }
    public static void main(String[] args) {
        DefaultExample7 p=new DefaultExample7();
        p.msg();
    }
}
输出:
Try to access the overridden method

示例8

让我们看一个示例,以确定是否使用私有访问修饰符将默认方法重写为子类。
class A {
    void msg() {
        System.out.println("try it");
    }
}
class DefaultExample8 extends A {
    private void msg() {
        System.out.println("try to access the overridden method");
    }
    public static void main(String[] args) {
        DefaultExample8 p=new DefaultExample8();
        p.msg();
    }
}
输出:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Cannot reduce the visibility of the inherited method from A

示例9

让我们看一个示例,以确定是否使用默认访问修饰符将默认方法重写为子类。
class A {
    void msg() {
        System.out.println("try it");
    }
}
class DefaultExample9 extends A {
    protected void msg() {
        System.out.println("try to access the overridden method");
    }
    public static void main(String[] args) {
        DefaultExample9 p=new DefaultExample9();
        p.msg();
    }
}
输出:
Try to access the overridden method

示例10

让我们看一个示例,以确定是否使用公共访问修饰符将默认方法重写为子类。
class A {
    void msg() {
        System.out.println("try it");
    }
}
class DefaultExample10 extends A {
    public void msg() {
        System.out.println("try to access the overridden method");
    }
    public static void main(String[] args) {
        DefaultExample10 p=new DefaultExample10();
        p.msg();
    }
}
输出:
Try to access the overridden method
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4