几个Java面向对象练习属性和方法的小例子

这篇博客通过多个Java代码示例介绍了面向对象编程的基础,包括创建Circle类计算圆的面积,使用Person类展示属性和方法的运用,以及实现PrintMatrix类打印矩阵并返回面积。此外,还展示了如何优化代码,将功能封装到Function类中,如查找特定年级学生、冒泡排序和遍历学生信息。这些例子涵盖了对象实例化、数组操作、方法封装和排序算法等核心概念。
摘要由CSDN通过智能技术生成

下面是几个学习Java面向对象的属性和方法过程中的几个练习的小例子,例子是先看到问题自己敲一遍代码,然后看一下自己代码能不能运行起来,如果能运行看一下自己的代码和视频中的差距,如果不能运行看一下是哪个地方出现了问题,在学习过程中,最后一个稍微复杂一点的例子中将方法封装在一个类中,如果想将另外一个类实例化的对象作为方法类中的形参,需要先将方法类实例化一个对象,然后在方法类的对象中将形参部分设置成实例化的对象。学到了学到了

	package OPPexer;
	/**
	 * 
	 * @Description类Circle计算圆的面积
	 * @author 
	 * @version 
	 * @date 2021年12月3日下午1:37:49
	 */
	public class Circle 
	{
	
	}
	//圆
	class Circle1
	{	//定义属性
		double ridus;
		
		//定义方法
		public double findArea()
		{
			double area=Math.PI*ridus*ridus;
			return area;
		}
	}
package OPPexer;

public class CircleTest 
{
	public static void main(String[] args) 
	{
		Circle1 c1=new Circle1();
		c1.ridus=2.1;
		double theArea=c1.findArea();
		System.out.println("面积为"+theArea);
	}
}

这两个类分文件编写,发现不用C++中的include<...>

确实方便,JavaYYDS

package OPPexer;
/**
 * 
 * @Description
 * @author 
 * @version 
 * @date 2021年12月3日下午1:19:11
 */
public class Exe1 
{
	public static void main(String[] args) 
	{	
		Person p1=new Person();
		p1.name="Tom";
		p1.age=3;
		p1.sex=1;
		p1.study();
		p1.showAge();
		int newAge=p1.addAge(2);
		System.out.println(p1.name+"的新年龄为:"+newAge);
		System.out.println(p1.age);//5 此时p1对象的年龄已经修改为5
	}
}
class Person
{
	String name;
	int age;
	/**
	 * sex取值1为男性,取值0为女性
	 */
	int sex;
	
	public void study()
	{
		System.out.println("studying!");
	}
	public void showAge()
	{
		System.out.println("age:"+age);
	}
	public int addAge(int i)
	{	
		age+=i;
		return age;
	}
}
package OPPexer;
/**
 * 
 * @Description
 * 打印m*n的*型矩阵,并返回矩阵的面积,在main方法中调用该方法
 * @author 
 * @version 
 * @date 2021年12月3日下午8:31:32
 */
public class Exe2 
{
	public static void main(String[] args) 
	{
		PrintMatrix m1=new PrintMatrix();
//		int area=m1.matrixMethod(10, 8);
//		System.out.println("面积为"+area); 第一种输出类型
		System.out.println(m1.matrixMethod(10, 8));//此时syso内括号里看作是返回值
	}
}
class PrintMatrix
{
	public int matrixMethod(int m,int n)
	{
		for(int i=0;i<m;i++)
		{
			for(int j=0;j<n;j++)
			{
				System.out.print("* ");
			}
			System.out.println();
		}
		return m*n;
	}
	
}

下面是比较长一点的例子,这个例子还是挺不错的,能复习数组、排序、已经面向对象的知识

package OOProgram;
/**
 * 
 * @Description
 * 定义类Student,包含三个属性,学号number(int),年纪state(int),成绩score(int)
 * 创建20个学生对象,学号为1-20,年级和成绩都由随机数确定
 * 打印年级为三年级(state为3)的学生信息
 * 利用冒泡排序对成绩进行排序,遍历所有学生信息
 * @author 
 * @version 
 * @date 2021年12月4日上午11:32:52
 */
public class ObjectArrayTest 
{
	public static void main(String[] args) 
	{
		Student stu[] = new Student[20];//定义一个Student类型的数组(对象数组)
		for(int i=0;i<stu.length;i++)
		{	stu[i]=new Student();//对数组的每个元素都new一个对象元素
			stu[i].number=i+1;
			stu[i].score=(int)(Math.random()*100+1);
			stu[i].state=(int)(Math.random()*10+1);
		}
		
		System.out.println("学号为"+stu[3].number+"年级为\t"+stu[3].state+"成绩为\t"+stu[3].score);
		
		//按照成绩进行排序
		for(int i=0;i<stu.length-1;i++)
		{
			for(int j=0;j<stu.length-1-i;j++)
			{
				if(stu[j].score>stu[j+1].score)
				{	
					//交换的数组中的元素:Student对象
					Student temp=stu[j];
					stu[j]=stu[j+1];
					stu[j+1]=temp;
				}
			}
		}
		for(int i=0;i<stu.length;i++)
		{
			System.out.println("年级为:"+stu[i].state+"学号为:"+stu[i].number+"成绩为"+stu[i].score);
		}
		System.out.println(stu[0]);//此时数组里面存放的是开辟的对象的地址
		
	}
}
class Student
{
	int number;//学号
	int state;//年级
	int score;//成绩
	
	
}

下面是对上述代码的优化,在main方法中显得代码非常整洁

package OOProgram;
/**
 * 
 * @Description
 * 定义类Student,包含三个属性,学号number(int),年纪state(int),成绩score(int)
 * 创建20个学生对象,学号为1-20,年级和成绩都由随机数确定
 * 打印年级为三年级(state为3)的学生信息
 * 利用冒泡排序对成绩进行排序,遍历所有学生信息
 * 对ObjectArrayTest.java代码的优化
 * @author 
 * @version 
 * @date 2021年12月4日上午11:32:52
 */
public class ObjectArrayTest1 
{
	public static void main(String[] args) 
	{
		Student1 stu[] = new Student1[20];//定义一个Student类型的数组(对象数组)
		for(int i=0;i<stu.length;i++)
		{	stu[i]=new Student1();//对数组的每个元素都new一个对象元素
			stu[i].number=i+1;
			stu[i].score=(int)(Math.random()*100+1);
			stu[i].state=(int)(Math.random()*10+1);
		}
        
		Function f=new Function();
		f.stateSearch(stu, 3);
		System.out.println("******************");
		f.printStudent(stu);
		System.out.println("******************");
		f.sort(stu);
		f.printStudent(stu);
		System.out.println(stu[0]);//此时数组里面存放的是开辟的对象的地址
		
	}
}
class Student1
{
	int number;//学号
	int state;//年级
	int score;//成绩

	
	
}
class Function
{
	/**
	 * 
	 * @Description 输出指定年级的学生信息 stu为要进行输出的集合整体,objState为目标年级
	 * @author NavyWang
	 * @date 2021年12月4日下午1:15:13
	 * @param stu
	 * @param objState
	 */
	public void stateSearch(Student1 [] stu,int objState)
	{
		for(int i=0;i<stu.length;i++)
		{
			if(stu[i].state==objState)
			{
				System.out.println("年级为:"+stu[i].state+" 学号为:"+stu[i].number+" 成绩为"+stu[i].score);
			}
		}
	}
	/**
	 * 
	 * @Description 利用冒泡排序按照成绩对对象排序
	 * @author NavyWang
	 * @date 2021年12月4日下午1:18:04
	 * @param stu
	 */
	public void sort(Student1[] stu)
	{
		for(int i=0;i<stu.length-1;i++)
		{
			for(int j=0;j<stu.length-1-i;j++)
			{
				if(stu[j].score>stu[j+1].score)
				{	
					//交换的数组中的元素:Student对象
					Student1 temp=stu[j];
					stu[j]=stu[j+1];
					stu[j+1]=temp;
				}
			}
		}
	}
	/**
	 * 
	 * @Description 遍历所有学生的信息
	 * @author NavyWang
	 * @date 2021年12月4日下午1:20:18
	 * @param stu
	 */
	public void printStudent(Student1[] stu)
	{
		for(int i=0;i<stu.length;i++)
		{
			System.out.print("年级为:"+stu[i].state+" 学号为:"+stu[i].number+" 成绩为"+stu[i].score);
			System.out.println();
		}
	}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值