属性与方法. this、super关键字

1. 属性与方法
属性和方法都是类的成员,用于描述类的特征,每个类都可以有若干个属性、若干个方法。
public class Sample {
}
属性用于描述可以使用值进行量化的特征,通常属性的名称会使用名词,例如:
public class Person {
  String from; // 默认为null
  String name; // 默认为null
  int height; // 默认为0
  float weight; // 默认为0.0f
}
方法用于描述动作或者行为,通常方法的名称是动词,例如:
public class Person {
  void eat() {
    // 东西怎么吃的?
  }
  void run() {
    // 怎么实现跑步的?
  }
}
方法的参数:参数是当执行方法时所需要的条件。
public class Sample {
  void sum(int x, int y) {
 
  }
}
当方法声明了参数后,调用该方法时,必须提供匹配数据类型、匹配数量、匹配位置的参数值,例如:
public class Test {
  public static void main(String[] a) {
    Sample sample = new Sample();
    // sample.sum(); // 错误
    // sample.sum(1234); // 错误
    // sample.sum("hello"); // 错误
    sample.sum(10, 20); // 正确
  }
}
每一个方法都可以有返回值,如果不需要返回值,则应该声明返回类型为void,如果需要,则声明为对应的类型,且在方法的最后,应该使用return语句返回对应类型的数据,例如:
public class Sample {
  int sum(int x, int y) {
    return x + y;
  }
}
当调用有返回值的方法时,可以获取其返回值,也可以不获取,例如:
public class Test {
  public static void main(String[] args) {
    Sample sample = new Sample();
    sample.sum(10, 15); // 仅调用,不获取值
    int a = sample.sum(3, 7); // 调用,且获取值
  }
}
对于声明为void返回值的方法,也可以使用return语句,但是,return关键字后不可以有任何值,例如:
public class Sample {
  void test() {
    return;
    // System.out.println(); // 无法被执行的语句,错误
  }
}
不管返回值是什么类型,return都应该是方法的最后一条有效语句。
为了便于开发,方法允许重载(Overload),即:在同一个类中允许存在若干个名称相同的方法,但是,这些方法的参数列表必须不同,例如:
public class Sample {
  void run() {}
  void run(int x) {}
  void run(String str) {}
  void run(String str1, String str2, int x) {}
  void run(int x, String str2, String str1) {}
  void run(String str1, int x, String str2) {}
}
参数列表的区别表现为:数量、数据类型、顺序

【测试题】
public class Test1 {
  public static void add1(int i) {
    i++;
  }
  public static void add2(int[] arr) {
    arr[0]++;
  }
  public static void main(String[] args) {
    int x = 10;
    add1(x);
    System.out.println("x=" + x); // 10
    int[] array = { 9, 5, 2, 7 };
    add2(array);
    System.out.println("array[0]=" + array[0]); // 10
  }
}
public class Test2 {
  public static void main(String[] args) {
    int x = 10;
    int y;
    y = x;
    y = 15;
    System.out.println("x=" + x); // 10
    System.out.println("y=" + y); // 15
    
    int[] arr1 = { 9, 5, 2, 7 };
    int[] arr2;
    arr2 = arr1;
    arr2[0] = 15;
    System.out.println("arr1[0]=" + arr1[0]); // 15
    System.out.println("arr2[0]=" + arr2[0]); // 15
  }
}


2. 构造方法
构造方法是一类特殊的方法,其特征是:
1) 构造方法不允许声明返回值类型,即连void都不可以
2) 构造方法的名称必须与类的名称完全相同
如果类中没有显式的声明构造方法,则编译器会自动的添加公有的、无参数的构造方法,例如以下2段代码是等效的:
public class Person {
}
public class Person {
  public Person() {
    super();
  }
}
构造方法用于创建类的对象,例如:
public class Test {
  public static void main(String[] args) {
    Person p = new Person();
  }
}
如果开发者显式的声明了构造方法,则编译器不会再自动添加构造方法。
构造方法可以用于创建对象时直接指定对象的某些属性值,例如没有构造方法时:
public class Person {
  public String name;
  public int age;
}
public class Test {
  public static void main(String[] args) {
    Person p = new Person();
    p.name = "Jack";
    p.age = 18;
  }
}
如果指定了构造方法:
public class Person {
  public String name;
  public int age;
 
  public Person(String personName, int personAge) {
    name = personName;
    age = personAge;
  }
}
public class Test {
  public static void main(String[] args) {
    Person p = new Person("Jack", 18);
  }
}
构造方法也可以重载,例如:
public class Person {
  public String name;
  public int age;
 
  public Person() {
  }
 
  public Person(String personName, int personAge) {
    name = personName;
    age = personAge;
  }
}


3. this、super关键字
this表示当前类的对象,super表示当前类的父类的对象。
public class Person extends Object {
  public String name;
 
  public Person() {
    this("unknown"); // 调用自身的另一个构造方法
  }
 
  public Person(String name) {
    super(); // 调用父类的构造方法
    this.name = name;
  }
 
  public String toString() {
    return null;
  }
 
  public void test1() {
    super.toString();
  }
 
  public void test2() {
    toString();
  }
 
}
public class Test {
  public static void main(String[] args) {
    Person jack = new Person("Jack");
    jack.name = "Jackson";
    // Person.name = "???"; // 普通属性不可以这样调用
    Person mike = new Person("Mike");
  }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值