为什么父类中的print()方法要使用private声明而不是public或protected?

2026-04-10 13:510阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计83个文字,预计阅读时间需要1分钟。

为什么父类中的print()方法要使用private声明而不是public或protected?

javaclass A { public void fun() { this.print(); } private void print() { // 此为private,无覆盖 System.out.println(zhe shi zsd); }}

public class TestDemo { public static void main(String[] args) { B b=new B(); b.fun(); }}

gistfile1.txt

class A { public void fun(){ this.print(); } private void print{ //此为private,无覆写 System.out.println("zhe shi zsd") } } public class TestDemo{ public static void main(String args[]){ B b = new B(); b.fun(); } }

为什么父类中的print()方法要使用private声明而不是public或protected?
标签:print方法

本文共计83个文字,预计阅读时间需要1分钟。

为什么父类中的print()方法要使用private声明而不是public或protected?

javaclass A { public void fun() { this.print(); } private void print() { // 此为private,无覆盖 System.out.println(zhe shi zsd); }}

public class TestDemo { public static void main(String[] args) { B b=new B(); b.fun(); }}

gistfile1.txt

class A { public void fun(){ this.print(); } private void print{ //此为private,无覆写 System.out.println("zhe shi zsd") } } public class TestDemo{ public static void main(String args[]){ B b = new B(); b.fun(); } }

为什么父类中的print()方法要使用private声明而不是public或protected?
标签:print方法