[Java] 纯文本查看 复制代码
package com.wz.extendsdemo;
class A {
public A() { // 父类无参构造
System.out.println("*************************") ;
}
}
class B extends A {
public B() { // 子类构造
System.out.println("#########################");
}
}
public class TestDemo {
public static void main(String args[]) {
B b = new B() ; // 实例化子类对象
}
}
[Java] 纯文本查看 复制代码
public class Person {
//属性
private String name;
private int age;
private char gender;
int i;
//构造方法
public Person(){}
public Person(String name, int age, char gender){
this.name = name;
this.age = age;
this.gender = gender;
}
//生成get/set方法
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;
}
public char getGender(){
return gender;
}
public void setGender(char gender){
this.gender = gender;
}
public String toString(){
return "name="+name+", age="+age+", gender="+gender;
}
//eat方法
public void eat(){
System.out.println("这是eat方法。。。");
}
//sleep方法
public void sleep(){
System.out.println("这是sleep方法。。。");
}
}
[Java] 纯文本查看 复制代码
public class Student extends Person{
//属性
private int score;
int i = 10;
//构造
public Student(){}
public Student(String name, int age, char gender, int score){
super(name, age, gender);
this.score = score;
}
//生成get/set方法
public int getScore(){
return score;
}
public void setScore(int score){
this.score = score;
}
public String toString(){
return "name="+getName()+", gender="+getGender()+", score="+score;
}
@Override
public void sleep() {
System.out.println("学生的sleep方法。。。");
}
public void score(){
System.out.println("学生的score方法。。。");
}
}
[Java] 纯文本查看 复制代码
public class Test {
public static void main(String[] args) {
//继承测试
Student stu = new Student("张三", 18, '男', 99);
stu.score();
stu.eat();
stu.sleep();
System.out.println(stu.getName());
System.out.println(stu.i);
}
}