//学习目标:面向对象类的继承 父类:Student 子类:Pupil
//作者:Kate
package com.oop;
class Stu{
//1.设置成员变量即静态属性
String name;
int age;
String sex;
double score;
//2.构造参数方法
public Stu(String name,int age,String sex,double score) {
this.name=name;
this.age=age;
this.sex=sex;
this.score=score;
System.out.println("调用四个参数构造函数");
}
public Stu() {
System.out.println("调用默认的构造函数");
}
public void showInfo() {
System.out.println("名字"+name+" 年龄"+age+" 性别"+sex+" 分数"+score);
}
}
//3.子类继承父类
class Pup extends Stu{
double weight;
public Pup(String name,int age,String sex,double score,double weight) {
super(name,age,sex,score);
this.weight=weight;
}
public void showData () {
//super.showInfo();
System.out.println("名字"+name+" 年龄"+age+" 性别"+sex+" 分数"+score+" 体重"+weight);
}
}
public class ExtendsTest {
public static void main(String[] args) {
Pup xiaobai=new Pup("xiaobai",12,"male",1232,242);
xiaobai.showInfo();
xiaobai.showData();
}
}
Java面向对象基础继承知识
文章展示了Java编程中面向对象的概念,具体是一个名为Pup的子类继承自Stu父类的示例。Pup类增加了weight属性,并重写了showData()方法来展示学生的信息包括姓名、年龄、性别、分数和体重。
摘要由CSDN通过智能技术生成