public class Hello { public static void main(String[] args) { Person bj = new Person(); String[] fullname = new String[]{"myself","c26"}; bj.setName(fullname); System.out.println(bj.getName()); } }
class Person{ private String[] name;
public String getName(){ return this.name[0]; } public void setName(String[] name){ this.name = name; }
public class Main { public static void main(String[] args) { Person p = new Person(); String bob = "Bob"; p.setName(bob); // 传入bob变量 System.out.println(p.getName()); // "Bob" bob = "Alice"; // bob改名为Alice p.setName(bob); // 传入bob变量 System.out.println(p.getName()); // "Bob"还是"Alice"? } }
class Person { private String name;
public String getName() { return this.name; }
public void setName(String name) { this.name = name; } }
public class Hello { public static void main(String[] args) { Person bj = new Person("myself",16); System.out.println(bj.getName()+ " " + bj.getAge()); } }
class Person{ private String name; private int age;
public Person(String name ,int age){ this.name = name; this.age = age; }
public String getName(){ return this.name; } public int getAge(){ return this.age; }
class Hello { public void hello() { System.out.println("Hello, world!"); }
public void hello(String name) { System.out.println("Hello, " + name + "!"); }
public void hello(String name, int age) { if (age < 18) { System.out.println("Hi, " + name + "!"); } else { System.out.println("Hello, " + name + "!"); } } }
这种方法名相同,但各自的参数不同,称为方法重载(Overload)。
注意:方法重载的返回值类型通常都是相同的。
方法重载的目的是,功能类似的方法使用同一名字,更容易记住,因此,调用起来更简单。
举个例子,String类提供了多个重载方法indexOf(),可以查找子串:
int indexOf(int ch):根据字符的Unicode码查找;
int indexOf(String str):根据字符串查找;
int indexOf(int ch, int fromIndex):根据字符查找,但指定起始位置;
int indexOf(String str, int fromIndex)根据字符串查找,但指定起始位置。
1 2 3 4 5 6 7 8 9 10 11
public class Main { public static void main(String[] args) { String s = "Test string"; int n1 = s.indexOf('t'); int n2 = s.indexOf("st"); int n3 = s.indexOf("st", 4); System.out.println(n1); System.out.println(n2); System.out.println(n3); } }
public class Main { public static void main(String[] args) { Person ming = new Person(); Person hong = new Person(); ming.setName("Xiao Ming"); // TODO: 给Person增加重载方法setName(String, String): hong.setName("Xiao", "Hong"); System.out.println(ming.getName()); System.out.println(hong.getName()); } } class Person { private String name;
public String getName() { return name; } public void setName(String name , String name1){ this.name = name + " " + name1 } public void setName(String name) { this.name = name; } }
继承
Java使用extends关键字来实现继承:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
class Person { private String name; private int age;
public String getName() {...} public void setName(String name) {...} public int getAge() {...} public void setAge(int age) {...} }
class Student extends Person { // 不要重复name和age字段/方法, // 只需要定义新增score字段/方法: private int score;
public int getScore() { … } public void setScore(int score) { … } }
public class Test { public static void main(String[] args){ show(new Cat()); show(new Dog()); // Animail a = new Cat(); // a.eat(); // Cat c = (Cat)a; // c.work(); } public static void show(Animail a){ a.eat(); if(a instanceof Cat){ Cat c = (Cat)a; c.work(); }else if(a instanceof Dog){ Dog c = (Dog)a; c.work(); } } }
abstract class Animail{ abstract void eat(); }
class Cat extends Animail{ public void eat(){ System.out.println("吃鱼"); } public void work(){ System.out.println("抓老鼠"); } }
class Dog extends Animail{ public void eat(){ System.out.println("吃骨头"); } public void work(){ System.out.println("看家"); } }
public class Main { public static void main(String[] args) { Person ming = new Person("Xiao Ming", 12); Person hong = new Person("Xiao Hong", 15); ming.number = 88; System.out.println(hong.number); hong.number = 99; System.out.println(ming.number); }WW }
class Person { public String name; public int age;
public static int number;
public Person(String name, int age) { this.name = name; this.age = age; } }