Java教程

Java public关键字

Java public关键字是访问修饰符。可以将其分配给变量,方法,构造函数和类。这是访问修饰符的最不受限制的类型。

Java public要记住的要点

public修饰符随处可见。因此,我们可以轻松地在类和包内外访问公众。 如果您要覆盖任何方法,则被覆盖的方法(即在子类中声明的方法)必须没有更多限制。因此,如果将public分配给任何方法或变量,则只能使用public access修饰符将该方法或变量覆盖为子类。 如果一个程序包含多个类,则最多可以将一个类分配为公共类。 如果一个类包含一个公共类,则程序的名称必须类似于该公共类的名称。

public关键字的示例

示例1

让我们看一个确定公共变量和方法是否可访问的示例还是不在课外在这里,我们还尝试在类外部创建构造函数的实例。
class A {
    public String msg="try to access a public variable outside the class";
    String info;
    public void display()
{
        System.out.println("try to access a public method outside the class");
        System.out.println(info);
    }
    public A(String info)
{
        this.info=info;
    }
}
public class PublicExample1 {
    public static void main(String[] args) {
        A a=new A("try to create the instance of public constructor outside the class");
        System.out.println(a.msg);
        a.display();
    }
}
输出:
try to access a public variable outside the class
try to access a public method outside the class
try to create the instance of public constructor outside the class

示例2

让我们看一个示例,以确定是否可以在包外部访问公共变量和方法。在这里,我们还尝试在包外部创建构造函数的实例。
//save by A.java
package com.java;
public class A {
    public String msg="try to access a public variable outside the package";
    String info;
    public void display()
 {
        System.out.println("try to access a public method outside the package");
        System.out.println(info);
    }
    public A(String info)
 {
        this.info=info;
    }
}
//save by PublicExample1.java
package com.lidihuo;
import com.java.A;
public class PublicExample2 {
    public static void main(String[] args) {
        A a=new A("try to create the instance of public constructor outside the package");
        System.out.println(a.msg);
        a.display();
    }
}
输出:
try to access a public variable outside the package
try to access a public method outside the package
try to create the instance of public constructor outside the package

示例3

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

示例4

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

示例5

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

示例6

让我们看一个示例,以确定是否使用受保护的访问修饰符将公共方法重写为子类。
class A
{
    public void msg()
{
        System.out.println("try it");
    }
}
class PublicExample6 extends A {
    protected void msg()
{
        System.out.println("try to access the overridden method");
    }
    public static void main(String[] args) {
        PublicExample6 p=new PublicExample6();
        p.msg();
    }
}
输出:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Cannot reduce the visibility of the inherited method from A
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4