1、Public类
(1)一个源文件中最多只能有一个public类,其他类的个数不限。
public class hello {
public static void main(String[] args) {
System.out.println("hello beijing!");
}
}
class one{}
class two{}
- 运行结果:生成三个类,每有一个都生成对应的class文件
(2)一个源文件包含public类,则文件名必须按该类名命名
// 文件名:Hello1.java
public class hello {
public static void main(String[] args) {
System.out.println("hello beijing!");
}
}
- 运行结果
(3)可将main方法写到非public类中,然后指定运行该类
class one{
public static void main(String[] args) {
System.out.println("hello one!");
}
}
class two{
public static void main(String[] args) {
System.out.println("hello two!");
}
}
- 运行结果
2、转义字符
- 常用转义字符
转义字符 | 字符描述 |
---|---|
\t | 制表符,实现对齐功能 |
\n | 换行符,将当前位置移动到下一行开头 |
\\ | \ |
\“ | ” |
\‘ | ’ |
\r | 回车,将当前位置移动到本行开头 |
- 范例
package com.lizhi;
public class hello {
public static void main(String[] args) {
System.out.println("1 hello \t beijing!");
System.out.println("2 hello \n beijing!");
System.out.println("3 hello \\ beijing!");
System.out.println("4 hello \" beijing!");
System.out.println("5 hello \' beijing!");
System.out.println("6 hello \r beijing!");
}
}
- 运行结果
3、注释概述
- 注释:提高代码阅读性
(1)单行注释
//单行注释
(2)多行注释
/* 注释内容 */
(3)文档注释
/**
*@author:小明
*@version:1.0
*/
4、代码规范
(1)命名
-
包名: 字母全小写 例如: cn.coderstory.Activity.Main
-
类,接口 :首字母大写,其他全小写 例如: class Container
-
方法,变量 :第二个单词开始首字母大写 例如: seedMessage
-
常量: 大写,单词用“_”分割 例如: final static MIN_WIDTH = 4
-
接口 :首字母大写 ,后缀Impl 例如: class ContainerImpl
-
异常类: 首字母大写, 后缀Exception 例如: DataNotFoundException
-
抽象类 :首字母大写, 前缀Abstract 例如: AbstractBeanDefinition
-
Test类: 首字母大写, 后缀Test 例如: public Location newLocation()
(2)注释
-
类/方法:以javadoc的方式来写
-
其余使用单行/多行注释
(3)编码
- 源文件使用utf-8编码