8.类与对象一举例、匿名对象
package com.atguigu.Demo;
/*
* 1.编写程序,声明一个method方法,在方法中打印一个10*8的*形矩阵,在main方法中调用该方法
* 2.修改上一个程序,在method方法中,除打印一个10*8的*形矩阵外,在计算该矩阵面积,并将其作为方法返回值。在main
* 方法中调用该方法,接受返回的面积值并打印
* 3.修改上一个程序,在method方法提供m和n两个参数,方法中打印一个m*n的*形矩阵,计算该矩阵面积,并将其作为方法返回值。在main
* 方法中调用该方法,接受返回的面积值并打印
*
*/
/*
* 1.编写程序,声明一个method方法,在方法中打印一个10*8的*形矩阵,在main方法中调用该方法
* 2.修改上一个程序,在method方法中,除打印一个10*8的*形矩阵外,在计算该矩阵面积,并将其作为方法返回值。在main
* 方法中调用该方法,接受返回的面积值并打印
* 3.修改上一个程序,在method方法提供m和n两个参数,方法中打印一个m*n的*形矩阵,计算该矩阵面积,并将其作为方法返回值。在main
* 方法中调用该方法,接受返回的面积值并打印
*
*/
/*题目1 public class Exer3 { public static void main(String[] args) { Exer3 test = new Exer3(); test.method(); } public void method() { for(int i = 0;i < 10;i++) { for(int j = 0; j < 8; j++) { System.out.print("*"); } System.out.println(); } } } */ /*题目二 public class Exer3 { public static void main(String[] args) { Exer3 test = new Exer3(); //int s = test.method(); //System.out.println(s); System.out.println(test.method()); } public int method() { for(int i = 0;i < 10;i++) { for(int j = 0; j < 8; j++) { System.out.print("*"); } System.out.println(); } int area = 10 * 8; return area; } } */ //题目3 public class Exer3 { public static void main(String[] args) { Exer3 test = new Exer3(); System.out.println(test.method(12, 10)); } public int method(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; } }
/* 定义类Student,包含三个属性:学号number(int),年级state(int),成绩score(int).创建20个学生对象,学号为1到20,年级和成绩都由随机数确定 问题一:打印出3年级的学生信息 问题二:使用冒泡排序,遍历所有学生信息 1)生成随机数:Math.random(),返回值类型double 2)四舍五入取整:Math.round(double d),返回值类型long */ public class StudentTest { public static void main(String[] args) { //声明一个Student类型的数组 对象数组 Student[] stus = new Student[20]; for(int i = 0; i < 20; i++) { //给数组元素赋值 每个元素创建一个实体对象 stus[i] = new Student(); //给Student对象的属性赋值 stus[i].number = (i + 1); //年级[1,6] stus[i].state =(int) (Math.random() * (6 - 1 + 1) + 1); //成绩[0,100] stus[i].score =(int) (Math.random() * (100 - 0 + 1)); } //遍历所有学生信息 for(int i = 0;i < stus.length; i++) { System.out.println(stus[i].Info()); //System.out.println(stus[i]);stu[i]存放 要么null 要么地址值 } System.out.println("*******************"); //题目一 for(int i = 0;i < stus.length; i++) { if(stus[i].state == 3) { System.out.println(stus[i].Info()); } } System.out.println("*******************"); for(int i = 0;i < stus.length - 1;i++) { for(int j = 0;j < stus.length - 1 - i;j++) { if(stus[j].score > stus[j+1].score) { //如果需要交换,交换的是数组元素:Studnet对象 Student temp = stus[j]; stus[j] = stus[j+1]; stus[j+1] = temp; } } } for(int i = 0;i < stus.length; i++) { System.out.println(stus[i].Info()); } } } class Student{ int number; int state; int score; //输出信息 public String Info() { return ("学号为:"+number +"年级为"+state+"成绩为"+score); } } /* public class StudentTest { public static void main(String[] args) { //声明一个Student类型的数组 对象数组 Student[] stus = new Student[20]; for(int i = 0; i < 20; i++) { //给数组元素赋值 每个元素创建一个实体对象 stus[i] = new Student(); //给Student对象的属性赋值 stus[i].number = (i + 1); //年级[1,6] stus[i].state =(int) (Math.random() * (6 - 1 + 1) + 1); //成绩[0,100] stus[i].score =(int) (Math.random() * (100 - 0 + 1)); } //遍历所有学生信息 for(int i = 0;i < stus.length; i++) { System.out.println("学号为:"+stus[i].number + "年级为"+stus[i].state+"成绩为"+stus[i].score); } } } class Student{ int number; int state; int score; } */
public class StudentTest1 { public static void main(String[] args) { //声明一个Student类型的数组 对象数组 Student1[] stus = new Student1[20]; for(int i = 0; i < 20; i++) { //给数组元素赋值 每个元素创建一个实体对象 stus[i] = new Student1(); //给Student对象的属性赋值 stus[i].number = (i + 1); //年级[1,6] stus[i].state =(int) (Math.random() * (6 - 1 + 1) + 1); //成绩[0,100] stus[i].score =(int) (Math.random() * (100 - 0 + 1)); } StudentTest1 test = new StudentTest1(); //遍历所有学生信息 test.print(stus); System.out.println("*******************"); test.searchState(stus, 3); System.out.println("*******************"); test.sort(stus); test.print(stus); } //遍历Student1[]数组操作 public void print(Student1[] stus) { for(int i = 0;i < stus.length; i++) { System.out.println(stus[i].Info()); } } /** * @Description 查找Student 数组中指定年级的学生信息 * @param stus 要查找的数组 * @param state 要查找的年即 */ public void searchState(Student1[] stus,int state) { for(int i = 0;i < stus.length; i++) { if(stus[i].state == state) { System.out.println(stus[i].Info()); } } } public void sort(Student1[] stus) { for(int i = 0;i < stus.length - 1;i++) { for(int j = 0;j < stus.length - 1 - i;j++) { if(stus[j].score > stus[j+1].score) { //如果需要交换,交换的是数组元素:Studnet对象 Student1 temp = stus[j]; stus[j] = stus[j+1]; stus[j+1] = temp; } } } } } class Student1{ int number; int state; int score; //输出信息 public String Info() { return ("学号为:"+number +"年级为"+state+"成绩为"+score); } }
理解“万事万物皆对象”
1.在java语言范畴中,我们都将功能、结构等封装到类中,通过类的实例化,来调用具体的功能结构。
>scanner、String 等
>文件file
>网络资源:URL
2.涉及到java语言与前端html、后端的数据库交互时,前后端的结构在java层面交互时,都体现为类、与对象。
匿名对象的使用
1.理解:我们创建的对象,没有显示的赋给一个变量名,即为匿名对象
2.特征:匿名对象只会调用一次
public class InstanceTest{ pubic static void main(String[] args){ Phone p = new Phone(); p.sendEmail(); p.playGame(); //匿名对象 new Phone().sendEmail(); } } class Phone{ double price;//价格 public void sendEmail(){ System.out.println("发邮件"); } public void playGame(){ System.out.println("玩游戏"); } }
/*
* 利用面向对象的编程,计算圆的面积
*/
package com.atguigu.Demo; public class CircleTest { public static void main(String[] args) { Circle c1 = new Circle(); c1.radius = 2.1; double s1 = c1.findArea(); System.out.println(s1); //system.out.println(c1.fingArea) } } class Circle{ double radius; public double findArea() { double area = radius * radius * Math.PI; return area; } //错误情况: 则定义属性多余 public double findArea(double r) { double area = 3.14 * r * r; return area; } } /* 方法二 public class CircleTest { public static void main(String[] args) { Circle c1 = new Circle(); c1.radius = 2.1; c1.findArea(); } } class Circle{ double radius; public void findArea() { double area =radius * radius * Math.PI; System.out.println("面积为:" + area); } } */
/*
* 要求:
*(1)创建Person类的对象,设置该对象的name、age、sex属性,
* 调用study方法,输出字符串“studying”,
* 调用showAge()方法显示age的值
* 调用addAge()方法给对象的age属性值增加2岁
*(2)创建第二个对象,执行上述操作,体会同一个类的不同对象之间的关系
*/
* 要求:
*(1)创建Person类的对象,设置该对象的name、age、sex属性,
* 调用study方法,输出字符串“studying”,
* 调用showAge()方法显示age的值
* 调用addAge()方法给对象的age属性值增加2岁
*(2)创建第二个对象,执行上述操作,体会同一个类的不同对象之间的关系
*/
package com.atguigu.Demo; public class PersonTest { public static void main(String[] args) { Person p1 = new Person(); p1.name = "Tom"; p1.age = 18; p1.sex = 1; p1.study(); p1.showAge(); //int newAge = p1.addAge(2); //System.out.println("新年龄:" + newAge); p1.addAge(2); p1.showAge(); Person p2 = new Person(); p2.showAge();//0 } } package com.atguigu.Demo; public class Person { String name; int age; /** * sex: 1表示男性 * sex: 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; } }
虽不能至,心向往之

浙公网安备 33010602011771号