先看下最終的結果吧,是不是你想要的?
其中,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