Java
Tips
- Java 程序由类组成。每个类都存储在一个单独的文件中,其文件名称与类名一致。该文件的扩展名为 java.
- 当我们有许多类文件时,我们会将它们分组到文件夹和子文件夹中。此外,类还会被分组到包和子包中。包和子包的名称必须在类代码中指定,并且这些名称必须与驱动器上的文件夹和子文件夹名称相同。
- 在 Java 语言中,方法不能单独存在。它必须在一个类中。因此,当人类需要用 Java 语言编写一个小程序时,他们必须首先创建一个类,在其中声明一个 main 方法,只有这样 才可以在该方法中编写代码。这些地球人太奇怪了!
- 合并字符串的规则很简单。如果我们将字符串和其他内容‘相加’(+),则‘其他内容’将通过 toString() 方法隐式地转换为字符串。
- 读取输入的字符串
//写法一:
InputStream inputStream = System.in;
Reader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String name = bufferedReader.readLine(); //从键盘读取字符串
String sAge = bufferedReader.readLine(); //从键盘读取字符串
int nAge = Integer.parseInt(sAge);
//写法二:
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String name = reader.readLine();
String sAge = reader.readLine();
int nAge = Integer.parseInt(sAge);
//写法三
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
int age = scanner.nextInt();
- getter and setter
class Cat {
public String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
- constructor Demo(this keyword)
public class Circle {
public double x;
public double y;
public double radius;
public Circle(double x, double y, double radius) {
this.x = x;
this.y = y;
this.radius = radius;
}
public Circle(double x, double y) {
//write your code here
this(x,y,10);
}
public Circle() {
this(5, 5, 1);
}
public static void main(String[] args) {
Circle circle = new Circle();
System.out.println(circle.x + " " + circle.y + " " + circle.radius);
Circle anotherCircle = new Circle(10, 5);
System.out.println(anotherCircle.x + " " + anotherCircle.y + " " + anotherCircle.radius);
}
}
未经本人准许,禁止转载.