属性和方法学生类的课堂练习

						学生类

package com.xuezhi.www.pojo;

import java.util.Scanner;

/**
*创建一个学生类 描述学生信息的一个类

  • @author Administrator

  • 类的构成:一般情况类是有两部分构成

  • 属性:直接在类中定义的,类似于之前定义的变量。在变量的基础上添加一个public访问修饰符。

  • 属性描述的是当前类的一个基本信息,属性不建议直接完成赋值,我们只需要声明出来。

  • 方法:封装了具体的一个功能实现。
    */
    public class Student {
    //1.添加学生基本的属性信息(学生姓名 学生年龄 学生学号 学生性别 爱好…)
    public String name;
    public int age;
    public int xh;
    public char sex;
    public String aihao;
    // //2.添加学生具备的功能(方法)信息
    // //方法的构成要素:1.访问修饰符(public) 2.方法的返回值类型(void) 3.方法名 4.方法参数列表 5.方法体
    // //定义一个显示学生基本信息的方法
    public void xs(){
    System.out.println(“学生的姓名是:”+name+",年龄是:"+age+",学号是:"+xh+",性别是:"+sex+",爱好是:"+aihao);
    }
    //定义一个学生具备学习能力的方法
    public void study(){
    System.out.println(“该学生具备学习的能力”);
    }

    //定义一个方法 实现姓名查找的功能
    //实现思路分析:
    //1.通过for循环动态的保存5个学生姓名到字符串数组中
    //2.通过控制台输入一个要查找的姓名 用字符串类型的变量接收
    //3.遍历数组通过查询的姓名去数组中查找 如果查找的姓名存在 就输出找到了 否则输出没找到

// public void cz(){
// Scanner input=new Scanner(System.in);
// String[] name=new String[5];
// String ans=“y”;
// //1.通过for循环动态的保存5个学生姓名到字符串数组中
// for(int i=0;i<name.length;i++){
// System.out.println(“请输入第”+(i+1)+“个学生的姓名:”);
// name[i]=input.next();
// }
// do{
// //2.通过控制台输入一个要查找的姓名 用字符串类型的变量接收
// System.out.println(“请输入要查找的姓名:”);
// String names=input.next();
// //3.遍历数组通过查询的姓名去数组中查找 如果查找的姓名存在 就输出找到了 否则输出没找到
// boolean falg=false;
// for(int i=0;i<name.length;i++){
// if(name[i].equals(names)){
// falg=true;
// break;
// }
// }
// if(falg){
// System.out.println(“找到了!”);
// }else{
// System.out.println(“没找到!”);
// }
// System.out.println(“是否继续查找(y/n):”);
// ans=input.next();
// }while(ans.equals(“y”));
// System.out.println(“下次在继续查找!”);
// }

//定义一个方法,实现姓名的查找,查找的姓名作为参数传递给当前方法

// public void cz(String name){
// boolean falg=false;
// //1.通过for循环动态的保存5个学生姓名到字符串数组中
// String[] xm=new String[5];
// for(int i=0;i<xm.length;i++){
// System.out.println(“请输入第”+(i+1)+“个学生的姓名:”);
// xm[i]=input.next();
// }
// //2.遍历数组通过查询的姓名去数组中查找,如果查找的姓名存在,返回true,如果查找的姓名不存在,返回false。
// for(int i=0;i<xm.length;i++){
// if(name.equals(xm[i])){
// falg=true;
// break;
// }
// }
// if(falg){
// System.out.println(“找到了!”);
// }else{
// System.out.println(“没找到!”);
// }
// }

												调用

package com.xuezhi.www.test;

import com.xuezhi.www.pojo.Student;

public class StudentTest {

/**
 * @param args
 */
//在当前类中(StudentTest)如何操作Sutdent类中的属性和方法?
//要操作一个类中的属性和方法 必须创建这个类的对象
//如何创建一个类的对象?
//创建一个类对象的语法: 类名  对象名 = new 类名();
//创建学生类对象 Student stu = new Student();
//如何通过对象去给类中的属性赋值和调用方法?
//给类中的属性完成赋值 需要通过对象名.属性的方式 调用属性完成赋值  
//比如给Student类中的年龄赋值  stu.age = 21;
//调用类中的方法 是通过对象名.方法名()
//比如调用Student类中的显示基本信息的方法  stu.showinfo();

//1.创建学生类的对象
//解决创建对象报错的原因:完成导包操作
public static void main(String[] args) {
	// TODO Auto-generated method stub
	//Student stu=new Student();
	//2.通过对象名.属性的方式 完成给属性赋值

// stu.name=“陌九”;
// stu.age=20;
// stu.xh=12345670;
// stu.sex=‘女’;
// stu.aihao=“看书”;
// //3.通过对象名.方法名() 调用方法 执行方法
// //如果没有通过对象名给类中的属性赋值 类中属性都有一个默认值
// //int类型默认值是0 String类型的默认值是null char类型是口 double类型的是0.0
// stu.xs();
// stu.study();

	//定义一个方法 实现姓名查找的功能
	//实现思路分析:
	//1.通过for循环动态的保存5个学生姓名到字符串数组中
	//2.通过控制台输入一个要查找的姓名 用字符串类型的变量接收
	//3.遍历数组通过查询的姓名去数组中查找 如果查找的姓名存在 就输出找到了 否则输出没找到
					//调用

// stu.cz();

	//对象数组:比如定义一个int类型的数组 当前是数组中保存的都是整数
	//根据如上描述 什么类型的数组 存放什么类型的数据
	//所以我们根据分析结果可以总结出来 对象类型的数组 存放的就是对象类型 
	//比如定义一个学生类型的对象对象 存放的就是学生对象
	
	//需求:定义一个学生类型的数组 存放5个学生对象
	
	Student[] stus=new Student[5];
	Student stu1=new Student();
	stu1.name="陌九";
	stu1.age=20;
	stu1.xh=123456;
	stu1.sex='女';
	stu1.aihao ="热爱冒险";
	Student stu2=new Student();
	stu2.name="言九";
	stu2.age=21;
	stu2.xh=123457;
	stu2.sex='男';
	stu2.aihao ="看书";
	Student stu3=new Student();
	stu3.name="苏九";
	stu3.age=23;
	stu3.xh=123459;
	stu3.sex='男';
	stu3.aihao ="绘画";
	Student stu4=new Student();
	stu4.name="萧瑟";
	stu4.age=22;
	stu4.xh=123451;
	stu4.sex='男';
	stu4.aihao ="百事通";
	Student stu5=new Student();
	stu5.name="无心";
	stu5.age=17;
	stu5.xh=123452;
	stu5.sex='男';
	stu5.aihao ="武功心法";
	stus[0] = stu1;
	stus[1] = stu2;
	stus[2] = stu3;
	stus[3] = stu4;
	stus[4] = stu5;
	for(int i=0;i<stus.length;i++){
		Student stu=stus[i];
		stu.xs();
	}

//定义一个方法,实现姓名的查找,查找的姓名作为参数传递给当前方法
//调用
String name=“张天师”;
stu.cz(name);

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值