//Student.class public class Student{ private String name; private int age; public static String teacherName; public Student(){} public Student(String name,int age){ this.name=name; this.age=age; } public void setName(String name){ this.name=name; } public String getName(){ return name; } public void setAge(int age){ this.age=age; } public int getAge(){ return age; }
public void show(){ System.out.println("学生姓名:"+name+" 学生年龄:"+age+" 老师姓名:"+teacherName); } }
//javabean类 //Student.java public class Student{ private String name; private int age; public Student(){} public Student(String name,int age){ this.name=name; this.age=age; } public void setName(String name){ this.name=name; } public String getName(){ return name; } public void setAge(int age){ this.age=age; } public int getAge(){ return age; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
//工具类 //StudentMaxAge.java import java.util.ArrayList; public class StudentMaxAge{ private StudentMaxAge(){} public static int age(ArrayList<Student> list){ int max=list.get(0).getAge(); for(int i=1;i<list.size();i++){ int n=list.get(i).getAge(); if(n>max){ max=n; } } return max; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
//测试类 //test.java import java.util.ArrayList; public class Test{ public static void main(String[] args){ Student student1=new Student("张三",18); Student student2=new Student("李四",19); Student student3=new Student("王五",17); ArrayList<Student> list=new ArrayList<>(); list.add(student1); list.add(student2); list.add(student3); int max=StudentMaxAge.age(list); System.out.println(max);//19 } }
注意事项
静态方法只能访问静态变量和静态方法
非静态方法可以访问静态变量或静态方法,也可以访问非静态的成员变量和非静态的成员方法
静态方法中没有this关键字
继承
定义:继承是面对对象三大特征之一,可以让类与类间产生子父关系。 好处:可以把多个子类重复的代码抽取到父类中,子类可直接使用,减少代码冗余,提高代码的复用性。 格式: public class 子类 extends 父类{} 继承后子类的特点: 子类可得到父类的属性和行为,子类可使用。 子类可在父类的基础上新增其他功能,子类更强大。
特点
JAVA只能单继承:一个类只能继承一个直接父类
JAVA不支持多继承:但支持多层继承
JAVA所有类都直接或间接继承于Object类
子类继承父类的内容
构造方法:无论是否私有,都不能继承
成员变量:无论是否私有,都能继承
注:private成员变量虽然能继承,但不可直接使用
成员方法:若虚方法表存有该方法,则可以继承,否则不能继承
虚方法表存储的方法:非private,非static,非final
继承中成员变量的访问特点
遵循就近原则,先在局部位置找,本类成员位置找,父类成员位置找,逐级往上。
范例
1 2 3 4
//父类 public class Father{ String name="Father"; }
1 2 3 4 5 6 7 8 9 10
//子类 public class Son extends Father{ String name="Son"; public void printName(){ String name="printName"; System.out.println(name);//从局部位置开始寻找,打印结果为printName System.out.println(this.name);//从本类成员位置开始寻找,打印结果为Son System.out.println(super.name);//从父类成员位置开始寻找,打印结果为Father } }
1 2 3 4 5 6 7 8 9 10 11
public class Test{ public static void main(String[] args){ Son son=new Son(); son.printName(); } } /* printName Son Father */
//父类 public class Dog { public void eat(){ System.out.println("吃狗粮"); } public void drink(){ System.out.println("喝水"); } public void lookHome(){ System.out.println("看家"); } }
//经理 public class Manager extends Employee{ private double manageSalary; public Manager(){} public Manager(String id,String name,int salary,double manageSalary){ super(id,name,salary); this.manageSalary=manageSalary; } public void setManageSalary(double manageSalary){ this.manageSalary=manageSalary; } public double getManageSalary(){ return manageSalary; } @Override public void work(){ System.out.println("管理其他人"); } }
//厨师 public class Cooker extends Employee{ public Cooker(){} public Cooker(String id,String name,int salary){ super(id,name,salary); } @Override public void work(){ System.out.println("炒菜"); } }
//Test.class public class Test{ public static void main(String[] args){ Person p=new Student(); if(p instanceof Teacher){ Teacher t=(Teacher)p; t.work(); }else if(p instanceof Student){ Student s=(Student)p; s.work(); }else{ System.out.println("没有这个类型"); } } }
//父类 //Person.class public class Person{ public void eat(){ System.out.println("吃饭"); } } //子类 //Teacher.class public class Teacher extends Person{ public void work(){ System.out.println("教学"); } } //Student.class public class Student extends Person{ public void work(){ System.out.println("听课"); } }
//父类 //Animal.class public class Animal{ private int age; private String color; public Animal(){}; public Animal(int age,String color){ this.age=age; this.color=color; } public void setAge(int age){ this.age=age; } public int getAge(){ return age; } public void setColor(String color){ this.color=color; } public String getColor(){ return color; } public void eat(String something){ System.out.println("吃"+something); } }
//Person.class public class Person{ private String name; private int age; public Person(){} public Person(String name,int age){ this.name=name; this.age=age; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
//子类 //Dog.class public class Dog extends Animal{ public Dog(){} public Dog(int age,String color){ super(age,color); } @Override public void eat(String something){ System.out.println(getAge()+"岁的"+getColor()+"颜色的狗两只前腿死死的抱住"+something+"猛吃"); } public void lookHome(){ System.out.println("看家"); } }
//Cat.class public class Cat extends Animal{ public Cat(){} public Cat(int age,String color){ super(age,color); } @Override public void eat(String something){ System.out.println(getAge()+"岁的"+getColor()+"颜色的猫眯着眼睛侧着头吃"+something); } public void catchMouse(){ System.out.println("抓老鼠"); } }
package com.ljsblog.domain1; public class Student{ private int age; private String name; static{ System.out.println("静态代码区"); } public Student(){ System.out.println("无参构造"); } public Student(int age,String name){ System.out.println("有参构造"); this.age=age; this.name=name; } public void setAge(int age){ this.age=age; } public int getAge(){ return age; } public void setName(String name){ this.name=name; } public String getName(){ return name; } }
1 2 3 4 5 6 7 8
package com.ljsblog.domain1; public class Test { public static void main(String[] args) { Student s1=new Student(); Student s2=new Student(18,"张三"); } }