方法
Java中所有的赋值和方法调用都是“按值“处理的,引用类型的值是对象的地址,原始类型的值是其自身。Java支持变长方法参数。
1 public class Program {
2
3 /**
4 * @param args
5 */
6 public static void main(String[] args) {
7 print("段光伟", "段光宇");
8 print(new String[] { "段光伟", "段光宇" });
9 }
10
11 private static void print(String... args) {
12 for (String item : args) {
13 System.out.println(item);
14 }
15 }
16 }
类
1 public class Program {
2
3 /**
4 * @param args
5 */
6 public static void main(String[] args) {
7 Point point = new Point(100);
8
9 System.out.print(point);
10 }
11 }
12
13 class Point {
14 private int x = 0;
15 private int y = 0;
16
17 public Point(int x, int y) {
18 this.x = x;
19 this.y = y;
20 }
21
22 public Point(int x) {
23 this(x, x);
24 }
25
26 public String toString() {
27 return "(x:" + this.x + ",y:" + this.y + ")";
28 }
29 }
注意:调用自身的构造方法是用this(xxx,xxx,...)来完成,且必须位于第一行。
https://www.bilibili.com/video/BV1qL411u7eE?spm_id_from=333.337.search-card.all.click
5902

被折叠的 条评论
为什么被折叠?



