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中类和对象-封装特性--练习的更多相关文章

  1. javaSE_07Java中类和对象-封装特性

    一.谈谈什么是面向对象的思维 理解面向对象,重点是要思考以下的问题 面向过程 vs 面向对象 Ø 谈谈什么是面向过程的编程思想? Ø 为什么有面向过程还要有面向对象? Ø 谈谈什么是面向对象的编程思想 ...

  2. javaSE_07Java中类和对象-封装特性-思维导图

    思维导图看不清楚时: 1)可以将图片另存为图片,保存在本地来查看 : 2)右击在新标签中打开放大查看 (IE不支持,搜狗,360可以):

  3. JS中类或对象的定义说明

    本篇文章主要是对JS中类或对象的定义进行说明介绍.我们知道,JS是面向对象的.谈到面向对象,就不可避免的要涉及类的概念.一般像c#,java这些强类型语言都有固定的定义类的语法.而JS的不同之处在于它 ...

  4. JS 对象封装的常用方式

    JS是一门面向对象语言,其对象是用prototype属性来模拟的,下面,来看看如何封装JS对象. 常规封装 function Person (name,age,sex){ this.name = na ...

  5. Javascript的对象封装和继承有哪些方法?优劣分别是什么?

    1.对象封装方法 (1)原始模式生成对象 直接将我们的成员写入对象中,用函数返回. 缺点:很难看出是一个模式出来的实例. function Stu(name, score) {             ...

  6. jfinal对象封装Record原理

    /*DbPro.class*/ public transient Record findFirst(String sql, Object paras[]{ List result = find(sql ...

  7. Ajax发送FormData对象封装的表单数据

    前端页面: <!doctype html> <html lang="en"> <head> <meta charset="UTF ...

  8. Swift --- 面向对象中类和对象的属性

    Swift中类和对象的属性分为三种:储存属性,计算属性和类属性. import Foundation class Person { // 储存属性必须赋初值 var score1: Int = 20 ...

  9. 利用js对象的特性,去掉数组中的重复项

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...

随机推荐

  1. MySQL之字符集-校对规则

    一.字符集(Character set) 是多个字符(英文字符,汉字字符,或者其他国家语言字符)的集合,字符集种类较多,每个字符集包含的字符个数不同. 特点: ①字符编码方式是用一个或多个字节表示字符 ...

  2. Spring+IOC(DI)+AOP概念及优缺点

    Spring pring是一个轻量级的DI和AOP容器框架. 说它轻量级有一大部分原因是相对与EJB的(虽然本人从没有接触过EJB的应用),重要的是,Spring是非侵入式的,基于spring开发的应 ...

  3. C字符串处理函数

    部分参考百科. C常用字符串函数:字符串输入函数,字符串输出函数,字符串处理函数,标准输入输出流 字符串处理函数: 1.字符串长度:strlen(str),返回字符串实际长度,不包括'\0',返回值类 ...

  4. EF的DbSet属性的Where查询,注意事项

    #1 Func<T,bool>与 Expression<Func<T,bool>>的区别 Func<T,bool>本身就是一个委托(delegate), ...

  5. 复选框选中删除行(DOM练习)

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  6. THINKPHP3.2 中使用 soap 连接webservice 解决方案

    今天使用THINKPHP3.2 框架中开发时使用soap连接webservice 一些浅见现在分享一下, 1.首先我们要在php.ini 中开启一下 php_openssl.dll php_soap. ...

  7. 在Mvc中进行异步请求是出现(没有为该对象定义无参数的构造函数)

    解决办法就是给相应的类添加无参数的构造函数:

  8. percona-xtrabackup安装

    二进制包安装(推荐安装方式,不用安装依赖包,非常方便): 1.下载安二进制包:      wget https://www.percona.com/downloads/XtraBackup/Perco ...

  9. Microsoft Windows 2003 SP2 - 'ERRATICGOPHER' SMB Remote Code Execution

    EDB-ID: 41929 Author: vportal Published: 2017-04-25 CVE: N/A Type: Remote Platform: Windows Aliases: ...

  10. 分布式服务:Dubbo+Zookeeper+Proxy+Restful 分布式架构

    分布式 分布式服务:Dubbo+Zookeeper+Proxy+Restful 分布式消息中间件:KafKa+Flume+Zookeeper 分布式缓存:Redis    分布式文件:FastDFS ...