javaSE_07Java中类和对象-封装特性--练习
1.编写封装一个学生类,有姓名,有年龄,有性别,有英语成绩,数学成绩,语文成绩,一个学生类,我们关注姓名,年龄,学历等信息,要求年龄必须在19-40岁之间,默认为19,学历必须是大专,本科,研究生这几个值的范围内,默认为大专。创建对象,测试其相关方法
类的创建:
/**
* 3.编写一个学生类,我们关注姓名,年龄,学历等信息,
* 要求年龄必须在19-40岁之间,默认为19,学历必须是大专,
* 本科,研究生这几个值的范围内,默认为大专。创建对象,测试其相关方法
* @author Administrator
*
*/
public class Student {
private String name;
private int age;
private String education;
//set get
public void setName(String name){
this.name=name;//这里必须加this,因为出现了局部变量和成员变量重名,必须指明name是哪个,下面同理
}
public String getName(){
return this.name;
}
public void setAge(int age){
if(age<19||age>40){
this.age=19;
}else{
this.age=age;
}
}
public int getAge(){
return this.age;
}
public void setEducation(String education){
if(education.equals("大专")||education.equals("本科")||education.equals("研究生")){
this.education=education;
}else{
this.education="大专";
}
}
public String getEducation(){
return this.education;
}
}
类的测试:
public class StudentTest {
public static void main(String[] args){
//1,创建对象
Student student=new Student();
//2,赋值
student.setName("张三");
student.setAge(28);
student.setEducation("本科");
//3,获取
System.out.println(student.getName());
System.out.println(student.getAge());
System.out.println(student.getEducation());
System.out.println();
//再创建一个对象.用于验证不符合条件情况
Student student2=new Student();
student2.setName("李四");
student2.setAge(10);
student2.setEducation("博士");
System.out.println(student2.getName());
System.out.println(student2.getAge());
System.out.println(student2.getEducation());
}
}
2.封装方法,求总分,平均分,最高分,最低分,以及打印学生的信息。创建对象,测试其相关方法
类的创建:
/**
* 4.封装一个学生类,有姓名,有年龄,有性别,有英语成绩,数学成绩,
* 语文成绩,封装方法,求总分,平均分,最高分,最低分,
* 以及打印学生的信息。创建对象,测试其相关方法
* @author Administrator
*
*/
public class Student2 {
private String name;
private int age;
private String sex;
private double english;
private double math;
private double chinese;
//set get
public void setName(String name){
this.name=name;
}
public String getName(){
return this.name;
}
public void setAge(int age){
this.age=age;
}
public int getAge(){
return this.age;
}
public void setSex(String sex){
this.sex=sex;
}
public String getSex(){
return this.sex;
}
public void setEnglish(double english){
this.english=english;
}
public double getEnglish(){
return this.english;
}
public void setMath(double math){
this.math=math;
}
public double getMath(){
return this.math;
}
public void setChinese(double chinese){
this.chinese=chinese;
}
public double getChinese(){
return this.chinese;
}
//构造函数实现直接给属性赋值
public Student2(){}
public Student2(String name,int age,String sex,double english,double math,double chinese){
this.setName(name);
this.setAge(age);
this.setSex(sex);
this.setEnglish(english);
this.setMath(math);
this.setChinese(chinese);
}
//getSum
public double getSum(){
return this.english+this.math+this.chinese;
}
//getAverage
public double getAverage(){
return this.getSum()/3;
}
//getMax
public double getMax(){
double max=this.english>this.math?this.english:this.math;
max=max>this.chinese?max:this.chinese;
return max;
}
//getMin
public double getMin(){
double min=this.english<this.math?this.english:this.math;
min=min<this.chinese?min:this.chinese;
return math;
}
}
类的测试:
public class Student2Test {
public static void main(String[] args){
Student2 student=new Student2("张飞",99,"男",88,77,79);
System.out.println(student.getName()+"成绩分析如下:");
System.out.println("总分:"+student.getSum());
System.out.println("平均分:"+student.getAverage());
System.out.println("最高分分:"+student.getMax());
System.out.println("最低分:"+student.getMin());
System.out.println();
//也可以和上一个例题一个个赋值
Student2 student2=new Student2();
student2.setName("刘备");
student2.setAge(78);
student2.setEnglish(99);
student2.setMath(88);
student2.setChinese(89);
System.out.println(student2.getName()+"成绩分析如下:");
System.out.println("总分:"+student2.getSum());
System.out.println("平均分:"+student2.getAverage());
System.out.println("最高分分:"+student2.getMax());
System.out.println("最低分:"+student2.getMin());
}
}
3.编写一个随机点名系统,两个功能,一个是抽取学员回答问题,一个是记录学员被命中的次数
类的创建:
package day09;
/**
* 编写一个随机点名系统,两个功能,
* 一个是抽取学员回答问题,一个是记录学员被命中的次数
* @author Administrator
*
*/
public class NameSystem {
private String name;
private int times;
//构造方法
public NameSystem(){
}
public NameSystem(String name,int times){
this.setName(name);
this.setTimes(times);
}
//set get
public void setName(String name){
this.name=name;
}
public void setTimes(int times){
this.times=times;
}
public String getName(){
return this.name;
}
public int getTimes(){
return this.times;
}
}
类的调用:
package day09;
import java.util.Scanner;
public class NameSystemTest {
public static void main(String[] args){
//1,创建对象
/*
NameSystem[] persons=new NameSystem[5];
persons[0].setName("张三");
persons[0].setTimes(0);
//动态赋值
persons[0]=new NameSystem("小李",0);
persons[1]=new NameSystem("小张",0);
persons[2]=new NameSystem("小王",0);
persons[3]=new NameSystem("小明",0);
persons[4]=new NameSystem("小刚",0);*/
//静态赋值
NameSystem[] persons={new NameSystem("小李",0),new NameSystem("小张",0),
new NameSystem("小王",0),new NameSystem("小明",0),new NameSystem("小刚",0)};
Scanner input=new Scanner(System.in);
String isContinue;
//2,点名
do{
int index=(int)(Math.random()*persons.length);
System.out.println("本次抽中的学员是:"+persons[index].getName());
persons[index].setTimes(persons[index].getTimes()+1);
System.out.println("继续请输入y,其他任意键退出");
isContinue=input.next();
}while(isContinue.equals("y"));
System.out.println("本次点名的结果统计:");
for(NameSystem p:persons){
System.out.println(p.getName()+":"+p.getTimes());
}
//关闭io流
input.close();
}
}
javaSE_07Java中类和对象-封装特性--练习的更多相关文章
- javaSE_07Java中类和对象-封装特性
一.谈谈什么是面向对象的思维 理解面向对象,重点是要思考以下的问题 面向过程 vs 面向对象 Ø 谈谈什么是面向过程的编程思想? Ø 为什么有面向过程还要有面向对象? Ø 谈谈什么是面向对象的编程思想 ...
- javaSE_07Java中类和对象-封装特性-思维导图
思维导图看不清楚时: 1)可以将图片另存为图片,保存在本地来查看 : 2)右击在新标签中打开放大查看 (IE不支持,搜狗,360可以):
- JS中类或对象的定义说明
本篇文章主要是对JS中类或对象的定义进行说明介绍.我们知道,JS是面向对象的.谈到面向对象,就不可避免的要涉及类的概念.一般像c#,java这些强类型语言都有固定的定义类的语法.而JS的不同之处在于它 ...
- JS 对象封装的常用方式
JS是一门面向对象语言,其对象是用prototype属性来模拟的,下面,来看看如何封装JS对象. 常规封装 function Person (name,age,sex){ this.name = na ...
- Javascript的对象封装和继承有哪些方法?优劣分别是什么?
1.对象封装方法 (1)原始模式生成对象 直接将我们的成员写入对象中,用函数返回. 缺点:很难看出是一个模式出来的实例. function Stu(name, score) { ...
- jfinal对象封装Record原理
/*DbPro.class*/ public transient Record findFirst(String sql, Object paras[]{ List result = find(sql ...
- Ajax发送FormData对象封装的表单数据
前端页面: <!doctype html> <html lang="en"> <head> <meta charset="UTF ...
- Swift --- 面向对象中类和对象的属性
Swift中类和对象的属性分为三种:储存属性,计算属性和类属性. import Foundation class Person { // 储存属性必须赋初值 var score1: Int = 20 ...
- 利用js对象的特性,去掉数组中的重复项
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...
随机推荐
- Android -- 带你从源码角度领悟Dagger2入门到放弃
1,以前的博客也写了两篇关于Dagger2,但是感觉自己使用的时候还是云里雾里的,更不谈各位来看博客的同学了,所以今天打算和大家再一次的入坑试试,最后一次了,保证最后一次了. 2,接入项目 在项目的G ...
- GitHub上最受欢迎的iOS开源项目TOP20
AFNetworking 在众多iOS开源项目中,AFNetworking可以称得上是最受开发者欢迎的库项目.AFNetworking是一个轻量级的iOS.Mac OS X网络通信类库,现在是GitH ...
- commitProperties方法
自定义的组件,如果重写commitProperties方法,那么在该方法内部一定要注意super.commitProperties()的调用.
- JS 部分基础内容总结
JavaScript 是脚本语言 HTML 中的脚本必须位于 <script> 与 </script> 标签之间. 脚本可被放置在 HTML 页面的 <body> ...
- iOS开发 - CocoaPods安装和使用教程
一.CocoaPods简介 1.什么是CocoaPods CocoaPods是iOS的包管理工具. 2.为什么要使用CocoaPods 在开发iOS项目时,经常会使用第三方开源库,手动引入流程复杂,并 ...
- 蓝桥杯-第一个数字-java
/* (程序头部注释开始) * 程序的版权和版本声明部分 * Copyright (c) 2016, 广州科技贸易职业学院信息工程系学生 * All rights reserved. * 文件名称: ...
- mac地址学习笔记
MAC(Media Access Control或者Medium Access Control)地址, 意译为媒体访问控制,或称为物理地址.硬件地址,用来定义网络设备的位置. 在OSI模型中,第三层网 ...
- 前端工作日常爬坑之——单页面微信开发Jssdk相关,以及jssdk图片直传自己服务器的实现。
日常爬坑 遇到的情况大致说明: 项目基于Vue2全家桶实现,vue-router控制前端路由,路由模式是History(主要是领导追求太高,觉得hash带#号太丑,然后遇到了小坑...),主要是服务于 ...
- CSS3特效----制作3D旋转照片展示区
任务一.制作多背景 提示:上层有一个径向渐变,渐变图像farthest-side ellipse at center, rgba(246,241,232,.85) 39%,rgba(212,204, ...
- ArcGIS 网络分析[1.5] 使用点线数据一起创建网络数据集(如何避免孤立点/点与线的连通性组合结果表)
ArcGIS中最基本的三种矢量数据是什么?点线面. 网络中除了路网之外,还会有地物点. 如上图,我们在建立网络数据集的时候,作为实验,当然可以只是公路网.但是在大型的决策任务中,网络数据集就不只是公路 ...