JAVA 定义Person类
public class Person {
private String name;
private int age;
Person(String name ,int age){
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return “name=”+name+”,age=”+age;
}
}
public class Student extends Person {
private String id;
private String school;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getSchool() {
return school;
}
public void setSchool(String school) {
this.school = school;
}
Student(String name, int age,String id ,String school) {
super(name, age);
this.id = id;
this.school=school;
}
public void Hello (){
System.out.println(“Hello”+ this.school + super.getName());
}
@Override
public String toString() {
return “school=”+this.school+”,id=”+this.id+”,”+super.toString();
}
}
public class Test {
public static void main(String[] args) {
Person person = new Person(“hanmeimei”, 21);
System.out.println(person);
Student student = new Student(“Lilei”, 22, “ID01”, “希望小学”);
System.out.println(student.toString());
}
}
java 定义一个类people属性域
People类:
import java.util.Date;
public class People {
String name;
Date birthday;
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public People()
{
}
public People(String s,Date date)
{
this.name=s;
this.birthday=date;
}
public void getAge()
{
Date today=new Date();
int age=today.getYear()-getBirthday().getYear();
System.out.println(“age = “+age);
}
/**
* @param args
*/
public static void main(String[] args) {
People p1=new People();
p1.setBirthday(new Date(87,1,2));
p1.getAge();
People p2=new People(“xiaoming”,new Date(90,1,2));
p2.getAge();
}
}
运行结果:
age = 23
age = 20
Student类:
import java.util.Date;
public class Student extends People {
String stuNo;
public String getStuNo() {
return stuNo;
}
public void setStuNo(String stuNo) {
this.stuNo = stuNo;
}
public Student(String s,Date date,String no)
{
super(s,date);
this.stuNo=no;
}
public void readStuNo()
{
System.out.println(“stuNo = “+getStuNo());
}
/**
* @param args
*/
public static void main(String[] args) {
Student p2=new Student(“xiaoming”,new Date(90,1,2),”20101000″);
p2.getAge();
p2.readStuNo();
}
}
运行结果:
age = 20
stuNo = 20101000
Java定义People类,它具有以下成员变量:String name,int age,它有两个构造方法!
public class People {
private String name;
private Integer age = 20;
public People(String name, Integer age) {
System.out.println(“A 1”);
this.name = name;
this.age = age;
}
public People(String name) {
System.out.println(“A 2”);
this.name = name;
new People(name, age);
}
public void work(){
System.out.println(“A work”);;
}
}
public class Teacher extends People{
public Teacher(String name) {
super(name);
}
public void work(){
System.out.println(“T are teach”);
}
}
public class Student extends People{
public Student(String name) {
super(name);
}
public void work(){
System.out.println(“Student are work”);
}
}
public class D {
public static void main(String[] args) {
Student s = new Student(“s1”);
s.work();
Teacher t = new Teacher(“t1”);
t.work();
}
}
Java把类people改为接口people?
其他的好说,people接口中,定义公共的 void speakHello(); 子类实现,并扩展自己特有的方法即可,但是 weight,height的考察点在哪???在接口中,变量相当于 直接被public final static 修饰且实例化了,也就是说没法修改,那么可以直接在实现类中定义。