JAVA中类的public class与class的区别详解
<h2 style="color: black; text-align: left; margin-bottom: 10px;">1.类的<span style="color: black;">拜访</span>权限</h2>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">为了<span style="color: black;">掌控</span>某个类的<span style="color: black;">拜访</span>权限,修饰词<span style="color: black;">必要</span>出<span style="color: black;">此刻</span><span style="color: black;">重要</span>字class之前。例如:public class Student {}在编写类的时候<span style="color: black;">能够</span><span style="color: black;">运用</span>两种方式定义类: </p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">(A)public class定义类 </p>(B)class定义类<h2 style="color: black; text-align: left; margin-bottom: 10px;">2.public class定义类</h2><span style="color: black;">倘若</span>一个类声明的时候<span style="color: black;">运用</span>了public class进行了声明,则类名<span style="color: black;">叫作</span><span style="color: black;">必要</span>与文件名<span style="color: black;">叫作</span>完全一致。被public修饰的类<span style="color: black;">能够</span>被其他包<span style="color: black;">拜访</span>。<span style="color: black;">倘若</span><span style="color: black;">此刻</span>的库名是com,<span style="color: black;">那样</span>就可容易<span style="color: black;">经过</span>下面的声明<span style="color: black;">拜访</span>Student:<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://pic1.zhimg.com/80/v2-7ffad5b2ca497cb401b25d2028fc90b0_720w.webp" style="width: 50%; margin-bottom: 20px;"></div>
<h2 style="color: black; text-align: left; margin-bottom: 10px;">3.class定义类</h2><span style="color: black;">倘若</span>一个类声明的时候<span style="color: black;">运用</span>了class进行了声明,则<span style="color: black;">做为</span><span style="color: black;">起步</span>类的名<span style="color: black;">叫作</span><span style="color: black;">能够</span>与文件名<span style="color: black;">叫作</span>不一致,<span style="color: black;">然则</span>执行的时候肯定执行的是生成后的名<span style="color: black;">叫作</span>。<span style="color: black;">无</span>public修饰的类,该类就<span style="color: black;">持有</span>了包<span style="color: black;">拜访</span>权限,即该类只<span style="color: black;">能够</span>用于该包之中。<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://pic3.zhimg.com/80/v2-757e4c90e3c807aada0575759c254ea2_720w.webp" style="width: 50%; margin-bottom: 20px;"></div>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://pic1.zhimg.com/80/v2-b6da5755c489cb96564de30c111905b4_720w.webp" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">4.class定义的类只<span style="color: black;">拥有</span>包<span style="color: black;">拜访</span>权限,该类<span style="color: black;">不可</span>被其他包<span style="color: black;">拜访</span></p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://pic3.zhimg.com/80/v2-aedc623972bee256033177fcfa580cd2_720w.webp" style="width: 50%; margin-bottom: 20px;"></div>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://pic2.zhimg.com/80/v2-4f28dd60b762504b4fe30ae0c0ca59a1_720w.webp" style="width: 50%; margin-bottom: 20px;"></div>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://pic3.zhimg.com/80/v2-9064c15a0698ce34f4837e21fb5fb9d6_720w.webp" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">5.小结</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">每一个</span>编译单元(文件)都只能有一个public类。即<span style="color: black;">每一个</span>编译单元都有单一的公共接口,用public类实现。此时,mian()就<span style="color: black;">必要</span>要<span style="color: black;">包括</span>在public类中。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> public类的名<span style="color: black;">叫作</span><span style="color: black;">必要</span>完全与含有该编译单元的文件名<span style="color: black;">叫作</span>一致,<span style="color: black;">包含</span><span style="color: black;">体积</span>写。<span style="color: black;">倘若</span>不匹配,编译时错误。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> <span style="color: black;">倘若</span>编译单元(文件)中不含有一个public类,此时编译单元文件名<span style="color: black;">叫作</span><span style="color: black;">能够</span>与<span style="color: black;">起步</span>类名<span style="color: black;">叫作</span><span style="color: black;">能够</span>不一致,即<span style="color: black;">能够</span>随意对文件命名。<span style="color: black;">这般</span>的话,main()不是<span style="color: black;">必要</span>要放在public类中<span style="color: black;">才可</span>运行程序。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">总的<span style="color: black;">来讲</span>,一个Java源文件中最多只能有一个public类,当有一个public类时,源文件名<span style="color: black;">必要</span>与之一致,否则<span style="color: black;">没</span>法编译,<span style="color: black;">倘若</span>源文件中<span style="color: black;">无</span>一个public类,则文件名与类中<span style="color: black;">无</span>一致性<span style="color: black;">需求</span>。至于main()不是<span style="color: black;">必要</span>要放在public类中<span style="color: black;">才可</span>运行程序。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">————————————————</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">版权声明:本文为CSDN博主「Joohong」的原创<span style="color: black;">文案</span>,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">原文链接:<a style="color: black;">JAVA中类的public class与class的区别详解</a></p>
系统提示我验证码错误1500次 \~゛,
页:
[1]