如何在 Java 中编写类名?


在编写类名时,你需要记住以下几点。

  • 不应该使用预定义或现有类名作为当前类的名称。
  • 不应使用任何 Java 关键字作为类名(大小写相同)。
  • 类名的第一个字母应大写,其余字母应小写(小写字母)。
class Sample
  • 同样,名称中每个词的第一个字母应大写,其余字母应小写。
class Test
  • 建议保持接口名称简单且有描述性。
  • 最好不要在编写类名时使用缩写。

示例

现场演示

public class Test {
   public void sample() {
      System.out.println("This is sample method");   
   }
   public void demo() {
      System.out.println("This is demo method");   
   }
   public static void main(String args[]) {
      Test obj = new Test();
      obj.sample();
      obj.demo();
   }
}

输出

This is sample method
This is demo method

更新于: 30-Jul-2019

5K+ 次观看

开启您的 职业 生涯

完成课程即可获得认证

开始使用
广告
© . All rights reserved.