先看下最终的结果吧,是不是你想要的?
其中,Student是父类,PostGraate是子类,继承自父类Student,Main是主类,用于创建对象以及把这些对象的功能调用起来。
---------------------------Student代码如下:------------------------------
/**
* 学生类
* @author 逍遥
*
*/
public class Student {
//学号
private int sId;
//姓名
private String sName;
//数学成绩
private double mathScore;
//计算机成绩
private double computerScore;
/**
* 获取学号
* @return
*/
public int getsId() {
return sId;
}
/**
* 设置学号
* @param sId
*/
public void setsId(int sId) {
this.sId = sId;
}
/**
* 获取姓名
* @return
*/
public String getsName() {
return sName;
}
/**
* 设置姓名
* @param sName
*/
public void setsName(String sName) {
this.sName = sName;
}
/**
* 获取数学成绩
* @return
*/
public double getMathScore() {
return mathScore;
}
/**
* 设置数学成绩
* @param mathScore
*/
public void setMathScore(double mathScore) {
this.mathScore = mathScore;
}
/**
* 获取计算机成绩
* @return
*/
public double getComputerScore() {
return computerScore;
}
/**
* 设置计算机成绩
* @param computerScore
*/
public void setComputerScore(double computerScore) {
this.computerScore = computerScore;
}
/**
* 输出成员变量(4个成员变量)的信息。
*/
public void print(){
System.out.println("学号:"+sId);
System.out.println("姓名:"+sName);
System.out.println("计算机成绩:"+mathScore);
System.out.println("数学成绩:"+computerScore);
}
}
---------------------------Student代码结束------------------------------
---------------------------PostGraate代码如下:------------------------------
/**
* 研究生类
* @author 逍遥
*
*/
public class PostGraate extends Student{
//导师姓名
private String tName;
//研究方向
private String ResearchDirection;
/**
* 获取导师姓名
* @return
*/
public String gettName() {
return tName;
}
/**
* 设置导师姓名
* @param tName
*/
public void settName(String tName) {
this.tName = tName;
}
/**
* 获取研究方向
* @return
*/
public String getResearchDirection() {
return ResearchDirection;
}
/**
* 设置研究方向
* @param researchDirection
*/
public void setResearchDirection(String researchDirection) {
ResearchDirection = researchDirection;
}
/**
* 研究生类重写父类的void print()方法,功能是输出成员变量(6个成员变量)的信息
*/
@Override
public void print() {
// TODO Auto-generated method stub
super.print();
System.out.println("导师姓名:"+tName);
System.out.println("研究方向:"+ResearchDirection);
}
}
---------------------------PostGraate代码结束------------------------------
---------------------------Main代码如下:------------------------------
import java.util.Scanner;
/**
* 主类
* @author 逍遥
*
*/
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//用于获取从键盘上输入的信息
Scanner input=new Scanner(System.in);
//创建一个Student类的对象
Student student=new Student();
//从键盘上输入其属性信息
System.out.print("请输入学生的学号:");
student.setsId(input.nextInt());
System.out.print("请输入学生的姓名:");
student.setsName(input.next());
System.out.print("请输入学生的数学成绩:");
student.setMathScore(input.nextDouble());
System.out.print("请输入学生的计算机成绩:");
student.setComputerScore(input.nextDouble());
//并且通过其print方法输出这些信息;
student.print();
//创建一个PostGraate类的对象
PostGraate postGraate=new PostGraate();
//从键盘上输入其属性信息
System.out.print("请输入研究生的学号:");
postGraate.setsId(input.nextInt());
System.out.print("请输入研究生的姓名:");
postGraate.setsName(input.next());
System.out.print("请输入研究生的数学成绩:");
postGraate.setMathScore(input.nextDouble());
System.out.print("请输入研究生的计算机成绩:");
postGraate.setComputerScore(input.nextDouble());
System.out.print("请输入研究生的导师姓名:");
postGraate.settName(input.next());
System.out.print("请输入研究生的研究方向:");
postGraate.setResearchDirection(input.next());
//并且通过其print方法输出这些信息。
postGraate.print();
}
}
---------------------------Main代码结束------------------------------
=================知识点的简单总结=================
本题考察的知识点是面向对象的三大特性之一:继承。
Student为父类,包含了学号、姓名、数学成绩和计算机成绩4个属性,以及一个print()方法。
PostGraate 继承父类的时候,继承了父类中的所有方法,因为方法我都是用的public,而属性继承不了,因为我在父类中用了封装,所有属性都用private修饰了,想访问属性的话,必须通过get、set方法,这里,我重写了父类中的print方法,通过super.print();调用了父类中的print()方法。
最后就是Main类,提供了main方法作为入口函数,用于按要求声明这些对象以及去调用对象中的方法。
B. java面向对象编程题目
public class Rectangle
{
private double width;
private double height;
//无参构造器
public Rectangle()
{}
//有参构造器
public Rectangle(double width, double height)
{
this.width = width;
this.height = height;
}
//属性的get和set方法定义
public void setWidth(double width)
{
this.width = width;
}
public double getWidth()
{
return this.width;
}
public void setHeight(double height)
{
this.height = height;
}
public double getHeight()
{
return this.height;
}
//计算周长的方法
private double getPerimeter()
{
return (width+height)*2;
}
//计算面积的方法
private double getArea()
{
return width*height;
}
public static void main(String[] args)
{
Rectangle rec = new Rectangle(3.6,5.8);
System.out.println("The perimeter of Rectangle is:"+rec.getPerimeter());
System.out.println("The area of Rectangle is:"+rec.getArea());
}
}
C. java面向对象编程题目。要求用抽象类和接口
//abstract Shape形状类
publicabstractclassShape{
abstractdouble area();
abstractdouble perimeter();
}
//Rectangle继承Shape类
{
private double width;
publicdoublegetWidth(){
returnwidth;
}
publicvoidsetWidth(doublewidth){
this.width=width;
}
publicdoublegetHeight(){
returnheight;
}
publicvoidsetHeight(doubleheight){
this.height=height;
}
private double height;
Rectangle(){
}
Rectangle(double width,double height){
this.width=width;
this.height=height;
}
public double area(){
return width*height;
}
public double perimeter(){
return (width+height)*2;
}
}
//Circle类继承抽象类Shape
publicclassCircleextendsShape{
private final double PI=3.14;
private double radius;
Circle(){
}
Circle(double radius){
this.radius=radius;
}
publicdoublegetRadius(){
returnradius;
}
publicvoidsetRadius(doubleradius){
this.radius=radius;
}
public doublearea(){
returnradius*radius*PI;
}
public doubleperimeter(){
returnradius*2*PI;
}
}
/Triangle类继承抽象类Shape
{
private double di;
private double high;
Triangle(){
}
Triangle(double di,double high){
this.di=di;
this.high=high;
}
publicdoublegetDi(){
returndi;
}
publicvoidsetDi(doubledi){
this.di=di;
}
publicdoublegetHigh(){
returnhigh;
}
publicvoidsetHigh(doublehigh){
this.high=high;
}
public doublearea(){
return di*high*1/2;
}
public doubleperimeter(){
return di*3;//限等边三角形
}
}
//定义接口Shape
public interfaceShape{
publicdouble area();
publicdouble perimeter();
}
//Rectangle类实现接口Shape
{
private double width;
publicdoublegetWidth(){
returnwidth;
}
publicvoidsetWidth(doublewidth){
this.width=width;
}
publicdoublegetHeight(){
returnheight;
}
publicvoidsetHeight(doubleheight){
this.height=height;
}
private double height;
Rectangle(){
}
Rectangle(double width,double height){
this.width=width;
this.height=height;
}
public double area(){
return width*height;
}
public double perimeter(){
return (width+height)*2;
}
}
//Circle类实现接口Shape
{
private final double PI=3.14;
private double radius;
Circle(){
}
Circle(double radius){
this.radius=radius;
}
publicdoublegetRadius(){
returnradius;
}
publicvoidsetRadius(doubleradius){
this.radius=radius;
}
public doublearea(){
returnradius*radius*PI;
}
public doubleperimeter(){
returnradius*2*PI;
}
}
//Triangle类实现接口Shape
{
private double di;
private double high;
Triangle(){
}
Triangle(double di,double high){
this.di=di;
this.high=high;
}
publicdoublegetDi(){
returndi;
}
publicvoidsetDi(doubledi){
this.di=di;
}
publicdoublegetHigh(){
returnhigh;
}
publicvoidsetHigh(doublehigh){
this.high=high;
}
public doublearea(){
return di*high*1/2;
}
public doubleperimeter(){
return di*3;//限等边三角形
}
}
//测试类ShapeTest
publicclassShapeTest{
publicstaticvoidmain(String[]args){
Circlec1=newCircle(3);//圆类有参初始化
Circlec2=newCircle();//圆类无参初始化
c2.setRadius(3);//初始化c2的半径
Squares1=newSquare(3);//方形类有参初始化
Squares2=newSquare();//方形类无参初始化
s2.setSide(5);//初始化s2的边长
Trianglet1=newTriangle(5,6);//三角类有参初始化
Trianglet2=newTriangle();//三角类无参初始化
t2.setDi(2);//初始化t2的底
t2.setHigh(6);//初始化t2的高
print("c1的面积:"+c1.area()+" c2的面积"+c2.area());
print("c1的周长:"+c1.perimeter()+" c2的周长"+c2.perimeter());
print("s1的面积:"+s1.area()+" s2的面积"+s2.area());
print("s1的周长:"+s1.perimeter()+" s2的周长"+s2.perimeter());
print("t1的面积:"+t1.area()+" t2的面积"+t2.area());
print("t1的周长:"+t1.perimeter()+" t2的周长"+t2.perimeter());
}
//定义静态打印方法
public static void print(Object object){
System.out.println(object);
}
}
//打印结果
c1的面积:28.26 c2的面积28.26
c1的周长:18.84 c2的周长18.84
s1的面积:9.0 s2的面积25.0
s1的周长:12.0 s2的周长20.0
t1的面积:15.0 t2的面积6.0
t1的周长:15.0 t2的周长6.0