Java习题----面向对象综合练习题

1.编写出一个通用的人员类(Person),该类具有姓名(Name)、年龄(Age)、性别(Sex)等域。然后对person类的继承得到一个学生类(Student),该类能够存放学生的5门课的成绩,并能求出平均成绩。最后在Main函数中对student类的功能进行验证。

import java.util.Scanner;

class Person {
	String name;
	int age;
	String sex;
	public Person() {}
	public Person(String name,int age,String sex) {
		this.name=name;
		this.age=age;
		this.sex=sex;
	}
}
class Student extends Person{
	int score[]=new int[5];
	public Student() {
		
	}
	public Student(String name,int age,String sex) {
		super(name,age,sex);
		score();
	}
	public void score() {
		Scanner sc=new Scanner(System.in);
		System.out.println("请输入五门课的成绩:");
		for(int i=0;i<score.length;i++) {
		 score[i]=sc.nextInt();
		}
	}
	public void getAvg() {
		int sum=0;
		for(int i=0;i<score.length;i++) {
			sum+=score[i];
		}
		float avg=sum/score.length;
		System.out.println(this.name+"同学的平均成绩是:"+avg);
	}
	
}
public class Test_avg {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Student s=new Student("张三",23,"男");
		s.getAvg();

	}

}

import java.util.Scanner;

class Hero{
	String name;
	int vitality;
	int grade;
	public Hero() {
		this.vitality=100;
		this.grade=1;
		nameInput();
		show();
	}
	public void nameInput() {
		Scanner sc=new Scanner(System.in);
		System.out.println("请输入英雄的名字:");
		name=sc.nextLine();
	}
	public void show() {
		System.out.println("姓名:"+name+"\n生命值:"+vitality+"\n等级"+grade);
		System.out.println();
	}
	public Hero(int type) {
		nameInput();
		if(type==1) {
			this.vitality=100;
			System.out.println("普通英雄创建成功");
		}else if(type==2) {
			this.vitality=200;
			System.out.println("高级英雄创建成功");
		}
		this.grade=1;
		show();
	}
	public void combat(Hero hero) {
		hero.vitality-=100;
		System.out.println(hero.name+"受到攻击,生命值减100");
		hero.show();
	}
	public void conbat(Hero hero,int type) {
		if(type==1) {
		hero.vitality-=50;
		System.out.println(hero.name+"受到轻度攻击,生命值减50");
		}else if(type==2) {
			hero.vitality-=80;
			System.out.println(hero.name+"受到轻度攻击,生命值减80");
		}else {
			hero.vitality-=100;
			System.out.println(hero.name+"受到轻度攻击,生命值减100");
		}
		hero.show();
	}
	
}

public class Text_hero {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Hero h=new Hero();
		Hero h1=new Hero();
		h.combat(h1);
		h.conbat(h1, 2);

	}

}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值