面向对象概念
面向对象分析方法分析问题的思路和步骤:
1.根据问题需要,选择问题所针对的现实世界中的实体。
2.从实体中寻找解决问题相关的属性和功能,这些属性和功能就形成了概念世界中的类。
3.把抽象的实体用计算机语言进行描述,形成计算机世界中类的定义。即借助某种程序语言,把类构造成计算机能够识别和处理的数据结构。
4.将类实例化成计算机世界中的对象。对象是计算机世界中解决问题的最终工具。
学习面向对象内容的三条主线
1.Java 类及类的成员:属性、方法、构造器、代码块、内部类
2.面向对象的三大特征:封装、继承、多态性、(抽象性)
3.其它关键字:this、super、static、final、abstract、interface、package、import 等
面向过程与面向对象
(我们学pyhon的时候就是用一种面向过程的思想,Java则是面向对象,这里做一个区分。)
1.面向过程:强调的是功能行为,以函数为最小单位,考虑怎么做。
① 打开冰箱
② 把大象装进冰箱
③ 把冰箱门关住
2.面向对象:强调具备了功能的对象,以类/对象为最小单位,考虑谁来做。
* 人{
* 打开(冰箱){
* 冰箱.开门();
* }操作(大象){
* 大象.进入(冰箱);
* }关闭(冰箱){
* 冰箱.关门();
* }
* }
*
* 冰箱{
* 开门(){
* }
* 关门(){
* }
* }
*
* 大象{
* 进入(冰箱){
* }
* }
*/
面向对象的两个要素:
1.类:对一类事物的描述,是抽象的、概念上的定义,类中基本元素有属性和方法。
2.对象:是实际存在的该类事物的每个个体,因而也称为实例(instance)。可以理解为:类= 抽象概念的人;对象= 实实在在的某个人面向对象程序设计的重点是类的设计;设计类,其实就是设计类的成员。
类与对象的创建及使用
创建类语法格式;
修饰符 class 类名{
属性声明;
方法声明;
}
创建对象语法:类名对象名= new 类名();
示例:
public class PersonTest {
public static void main(String[] args) {
//创建对象语法:类名对象名= new 类名();
Person p1 = new Person();
//调用类的结构:属性、方法
//调用属性:“对象.属性”
p1.name = "Tom";
p1.age = 25;
p1.isMale = true;
System.out.println(p1.name);
//调用方法:“对象.方法”
p1.eat();
p1.sleep();
p1.talk("chinese");
//**********************
Person p2 = new Person();
System.out.println(p2.name); //null
System.out.println(p2.isMale);
//**********************
//将 p1 变量保存的对象地址值赋给 p3,导致 p1 和 p3 指向了堆空间中的一个对象实体。
Person p3 = p1;
System.out.println(p3.name);
p3.age = 10;
System.out.println(p1.age); //10
}
}
class Person{
//属性:对应类中的成员变量
String name;
int age;
boolean isMale;
//方法:对应类中的成员方法
public void eat(){
System.out.println("吃饭");
}
public void sleep(){
System.out.println("睡觉");
}
public void talk(String language){
System.out.println("人可以说话,使用的是:" + language);
}
}
到这里我感觉类就是在python里面一个装了很多函数的大函数,对象就是里面一个个小函数。当然只是比喻了。
对象的创建和使用:内存解析
这里我们只浅了解一下堆、栈和方法区:
- 堆(Heap),此内存区域的唯一目的就是存放对象实例,几乎所有的对象实例都在这里分配内存。这一点在 Java虚拟机规范中的描述是:所有的对象实例以及数组都要在堆上分配。
- 通常所说的栈(Stack),是指虚拟机栈。虚拟机栈用于存储局部变量等。局部变量表存放了编译期可知长度的各种基本数据类型(boolean、byte、char、short、int、float、long、double)、对象引用(reference类型,它不等同于对象本身,是对象在堆内存的首地址)。方法执行完,自动释放。
- 方法区(MethodArea),用于存储已被虚拟机加载的类信息、常量、静态变量、即时编译器编译后的代码等数据。
案例1:
用类创建的变量储存在栈中,创建的属性储存在堆中。p1和p2使用同一个类生成同一套属性方法,两个改动互不影响。其中p3是p1赋值的,所以p3和p1共用一个地址值,相互改动会有影响。
Person p1= new Person();
p1.name = "Tom";
p1.isMale = true;
Person p2 = new Person();
System.out.println(p2.name);//null
Person p3 = p1;
p3.age = 10;
类的成员之一:属性
属性vs局部变量(属性属于成员变量)
相同点:
- 定义变量的格式:数据类型 变量名 = 变量值
- 先声明,后使用
- 变量都有其对应的作用域
不同点:
1.在类中声明的位置不同
- 属性:直接定义在类的一对{}内
- 局部变量:声明在方法内、方法形参、构造器形参、构造器内部的变量
2.关于权限修饰符的不同
- 属性:可以在声明属性时,指明其权限,使用权限修饰符。常用的权限修饰符:private、public、缺省、protected,目前声明属性时,都使用缺省即可。缺省就是不加任何修饰符。
- 局部变量:不可以使用权限修饰符。
3 .默认初始化值的情况:
-
属性:类的属性,根据其类型,都有默认初始化值。
整型(byte、short、int、long):0
浮点型(float、double):0.0
字符型(char):0(或‘\u0000’)
布尔型(boolean):false
引用数据类型(类、数组、接口):null -
局部变量:没有默认初始化值
意味着:在调用局部变量之前,一定要显式赋值。
特别地:形参在调用时,赋值即可。例,45 行
4 .在内存中加载的位置,亦各不相同。
- 属性:加载到堆空间中(非 static)
- 局部变量:加载到栈空间
public class UserTest {
public static void main(String[] args) {
User u1 = new User();
System.out.println(u1.name);
System.out.println(u1.age);
System.out.println(u1.isMale);
u1.talk("俄语");
}
}
class User{
//属性(或成员变量)
String name; //不加 private 即为缺省
public int age; //不加 public 即为缺省
boolean isMale;
public void talk(String language){//language:形参,也是局部变量
System.out.println("我们使用" + language + "进行交流。");
}
public void eat(){
String food = "石头饼"; //石头饼:局部变量
System.out.println("北方人喜欢吃:" + food);
}
}
类的成员之二:方法
类中方法的声明和使用
方法:描述类应该具有的功能。
- 比如:Math类:sqrt()\random() …
- Scanner类:nextXxx() …
- Arrays类:sort() \ binarySearch() \ toString() \ equals() \ …
1.举例:
public void eat(){}
public void sleep(int hour){}
public String getName(){}
public String getNation(String nation){}
2. 方法的声明:权限修饰符 返回值类型 方法名(形参列表){方法体}
注意:static、final、abstract 来修饰的方法,后面再学。
3.权限修饰符:默认方法的权限修饰符前期先都使用public,Java规定的4种权限修饰符:private、public、缺省、protected -->封装性之后再细嗦。
4.返回值:如果方法有返回值,则必须在方法声明时,指定返回值的类型。同时,方法中,需要使用return关键字来返回指定类型的变量或常量:“return 数据”。
如果方法没有返回值,则方法声明时,使用void来表示。通常,没有返回值的方法中,就不需要使用return。但是,如果使用的话,只能“return;”表示结束此方法的意思。
例如:有返回值:
public String getNation(String nation){
String info = "我的国籍是:" + nation;
return info;
无返回值:
public void eat(){
System.out.println("客户吃饭");
return;
//return后不可以声明表达式
// System.out.println("hello");
}
public void sleep(int hour){
System.out.println("休息了" + hour + "个小时");
eat();//可以调用类中其他的方法
}
练习1
创建Person类:
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;
}
}
测试类:
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(p1.name + "的年龄为" + newAge);
System.out.println(p1.age); //20
//*******************************
Person p2 = new Person();
p2.showAge(); //0
p2.addAge(10);
p2.showAge(); //10
p1.showAge(); //20
}
}
练习2
利用面向对象的编程方法,设计类Circle计算圆的面积。
//测试类
public class CircleTest {
public static void main(String[] args) {
Circle c1 = new Circle();
c1.radius = 2.1;
//对应方式一:
// double area = c1.findArea();
// System.out.println(area);
//对应方式二:
c1.findArea();
}
}
//圆:3.14*r*r
class Circle{
//属性
double radius;
//圆的面积方法
//方法1:
// public double findArea(){
// double area = 3.14 * radius * radius;
// return area;
// }
//方法2:
public void findArea(){
double area = Math.PI * radius * radius;
System.out.println("面积为:" + area);
}
}
练习3
- 3.1 编写程序,声明一个method方法,在方法中打印一个108的型矩形,在main方法中调用该方法。
- 3.2修改上一个程序,在method方法中,除打印一个108的型矩形外,再计算该矩形的面积,
并将其作为方法返回值。在main方法中调用该方法,接收返回的面积值并打印。 - 3.3 修改上一个程序,在method方法提供m和n两个参数,方法中打印一个mn的型矩形,并计算该矩形的面积,将其作为方法返回值。在main方法中调用该方法,接收返回的面积值并打印。
一:
public class Circle {
public static void main(String[] args) {
sb ear = new sb();
ear.methon();
}
}
class sb{
public void methon(){
for (int i = 0;i<10;i++){
for (int j = 0;j<8;j++){
System.out.print("*");
}
System.out.println();
}
}
}
二:
public class Circle {
public static void main(String[] args) {
sb ear = new sb();
System.out.println(ear.method());
}
}
class sb{
public int method(){
for(int i = 0;i < 10;i++){
for(int j = 0;j < 8;j++){
System.out.print("* ");
}
System.out.println();
}
return 10 * 8;
}
}
三:
public class Circle {
public static void main(String[] args) {
sb ear = new sb();
System.out.println(ear.method(8,8));
}
}
class sb{
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;
}
}
练习4
- 对象数组题目:定义类Student,包含三个属性:学号number(int),年级state(int),成绩score(int)。
创建20个学生对象,学号为1到20,年级和成绩都由随机数确定。
- 问题一:打印出3年级(state值为3)的学生信息。
- 问题二:使用冒泡排序按学生成绩排序,并遍历所有学生信息
- 提示: 1) 生成随机数:Math.random(),返回值类型double; 2) 四舍五入取整:Math.round(double d),返回值类型long。
public class Circle {
public static void main(String[] args) {
Student[] stu = new Student[20];
for (int i = 0;i< stu.length;i++){
stu[i] = new Student();
stu[i].number = i+1;
stu[i].state = (int)(Math.random()*(6-1+1)+1);
stu[i].score = (int)(Math.random()*(100-0+1));
}
for (int i = 0;i< stu.length;i++){
// System.out.println(stu[i].number+","+stu[i].state+","+stu[i].score);
System.out.println(stu[i].info());
}
System.out.println("*************************");
for (int i = 0;i< stu.length;i++){
if (stu[i].state == 3){
System.out.println(stu[i].info());
}
}
System.out.println("***************************");
for (int i=0;i< stu.length-1;i++){
for (int j=0;j< stu.length-i-1;j++){
if (stu[j].score>stu[j+1].score){
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].info());
}
}
}
class Student{
int number;
int state;
int score;
public String info(){
return "学号:" + number +"年级:"+state + "成绩"+score;
}
}
改进版:将操作数组的功能封装到方法中。其中方法也可以封装在含主函数的那个类中。
public class Circle {
public static void main(String[] args) {
Student[] stu = new Student[20];
for (int i = 0;i< stu.length;i++){
stu[i] = new Student();
stu[i].number = i+1;
stu[i].state = (int)(Math.random()*(6-1+1)+1);
stu[i].score = (int)(Math.random()*(100-0+1));
}
Circle test = new Circle();
//遍历学生数组
test.print(stu);
//筛选3年纪
System.out.println("****************************************************");
test.searchState(stu,3);
System.out.println("************************************************************");
//冒泡排序
test.sort(stu);
for (int i = 0; i< stu.length;i++){
System.out.println(stu[i].info());
}
}
//遍历Student[]数组的操作
public void print(Student[] stu){
for (int i = 0;i< stu.length;i++){
// System.out.println(stu[i].number+","+stu[i].state+","+stu[i].score);
System.out.println(stu[i].info());
}
}
//查找Student数组中指定年级的学习信息
public void searchState(Student[] stu,int state){
for (int i = 0;i< stu.length;i++){
if (stu[i].state == 3){
System.out.println(stu[i].info());
}
}
}
//给Student数组排序
public void sort(Student[] stu){
for (int i=0;i< stu.length-1;i++){
for (int j=0;j< stu.length-i-1;j++){
if (stu[j].score>stu[j+1].score){
Student temp = stu[j];
stu[j] = stu[j+1];
stu[j+1] = temp;
}
}
}
}
}
class Student{
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.特征:匿名对象只能调用一次。(每使用一次匿名对象,就创建了一个新的对象)
3.使用:
public class InstanceTest {
public static void main(String[] args) {
Phone p = new Phone();
System.out.println(p);
p.sendEmail();
p.playGame();
//匿名对象
new Phone().price = 1999;
new Phone().showPrice(); //0.0
//*******************************
PhoneMall mall = new PhoneMall();
// mall.show(p);
//匿名对象的使用
mall.show(new Phone());
}
}
class PhoneMall{
public void show(Phone phone){
phone.sendEmail();
phone.playGame();
}
}
class Phone{
double price; //价格
public void sendEmail(){
System.out.println("发邮件");
}
public void playGame(){
System.out.println("打游戏");
}
public void showPrice(){
System.out.println("手机价格为:" + price);
}
}
对象数组的内存解析
/*引用类型的变量,只可能存储量两类值:null或地址值(含变量类型)*/
Student[] stus= new Student[5];
stus[0] = new Student();
sysout(stus[0].state);//1
sysout(stus[1]);//null
sysout(stus[1].number);//异常
stus[1] = new Student();
sysout(stus[1].number);//0
class Student{
int number;//学号
int state = 1;//年级
int score;//成绩
}
stus[0] = new Student();这里是创建匿名对象,既然是创建对象就会在堆空间再创建一套属性。
sysout(stus[1]);是null是因为第一行new了一个数组,数组的默认初始值是null。
自定义数组的工具类
public class ArrayUtil {
// 求数组的最大值
public int getMax(int[] arr) {
int maxValue = arr[0];
for (int i = 1; i < arr.length; i++) {
if (maxValue < arr[i]) {
maxValue = arr[i];
}
}
return maxValue;
}
// 求数组的最小值
public int getMin(int[] arr) {
int minValue = arr[0];
for (int i = 1; i < arr.length; i++) {
if (minValue > arr[i]) {
minValue = arr[i];
}
}
return minValue;
}
// 求数组总和
public int getSum(int[] arr) {
int sum = 0;
for (int i = 0; i < arr.length; i++) {
sum += arr[i];
}
return sum;
}
// 求数组平均值
public int getAvg(int[] arr) {
int avgValue = getSum(arr) / arr.length;
return avgValue;
}
// 反转数组
public void reverse(int[] arr) {
for (int i = 0; i < arr.length / 2; i++) {
int temp = arr[i];
arr[i] = arr[arr.length - i - 1];
arr[arr.length - i - 1] = temp;
}
}
// 复制数组
public int[] copy(int[] arr) {
int[] arr1 = new int[arr.length];
for (int i = 0; i < arr1.length; i++) {
arr1[i] = arr[i];
}
return null;
}
// 数组排序
public void sort(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr.length - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
// 遍历数组
public void print(int[] arr) {
System.out.print("[");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + ",");
}
System.out.println("]");
}
// 查找指定元素
public int getIndex(int[] arr, int dest) {
//线性查找
for (int i = 0; i < arr.length; i++) {
if (dest==arr[i]) {
return i;
}
}
return -1;
}
}
测试类:
/**
* @Description 测试类
*
*/
public class ArrayUtilTest {
public static void main(String[] args) {
ArrayUtil util = new ArrayUtil();
int[] arr = new int[]{32,5,26,74,0,96,14,-98,25};
int max = util.getMax(arr);
System.out.println("最大值为:" + max);
// System.out.print("排序前:");
// util.print(arr);
//
// util.sort(arr);
// System.out.print("排序后:");
// util.print(arr);
System.out.println("查找:");
int index = util.getIndex(arr, 5);
if(index > 0){
System.out.println("找到了,索引地址:" + index);
}else{
System.out.println("没找到");
}
}
}
方法的重载
概念:在同一个类中,允许存在一个以上的同名方法,只要它们的参数个数或者参数类型不同即可。
“两同一不同”:同一个类、相同方法名,参数列表不同:参数个数不同,参数类型不同
举例:
// 举例一:Arrays类中重载的sort() / binarySearch();PrintStream中的println()
// 举例二:
// 如下的4个方法构成了重载
public void getSum(int i,int j){
System.out.println("1");
}
public void getSum(double d1,double d2){
System.out.println("2");
}
public void getSum(String s ,int i){
System.out.println("3");
}
public void getSum(int i,String s){
System.out.println("4");
}
快速判断是否重载的方法:
严格按照定义判断:两同一不同。跟方法的权限修饰符、返回值类型、形参变量名、方法体都没关系!
可变个数的形参
JDK 5.0以前:采用数组形参来定义方法,传入多个同一类型变量
public static void test(int a, String[] books);
JDK 5.0以后:采用可变个数形参来定义方法,传入多个同一类型变量
public static void test(int a, String … books);
可变个数形参的方法
具体使用:
1.可变个数形参的格式:数据类型 … 变量名
2.当调用可变个数形参的方法时,传入的参数的个数可以是:0个,1个,2个…
3.可变个数形参的方法与本类中方法名相同,形参不同的方法之间构成重载。
4.可变个数形参的方法与本类中方法名相同,形参类型也相同的数组之间不构成重载。即二者不可共存。
5.可变个数形参在方法中的形参中,必须声明在末尾。
6.可变个数形参在方法中的形参中,最多只能声明一个可变形参。
public class MethodArgs {
public static void main(String[] args) {
MethodArgs test = new MethodArgs();
test.show(12);
test.show(new String[] { "AA", "BB", "CC" });
}
public void show(int i) {
}
public void show(String... strs) {
System.out.println("show(String ...strs)");
for (int i = 0; i < strs.length; i++) {
System.out.println(strs[i]);
}
}
// 此方法与上一方法不可共存
// public void show(String[] strs){
//
// }
//就是这个可变参数如果放在前面,在调用的时候就不知道哪个是可变参数哪个不是。
public void show(int i, String... strs) {
}
//The variable argument type String of the method show must be the last parameter
// public void show(String... strs,int i,) {
}
}
Java的值传递机制
规则: (重点)
如果变量是基本数据类型,此时赋值的是变量所保存的数据值。
如果变量是引用数据类型,此时赋值的是变量所保存的数据的地址值。
针对方法内变量的赋值举例:
public class ValueTransferTest {
public static void main(String[] args) {
System.out.println("**********基本数据类型:***********");
int m = 10;
int n = m;
System.out.println("m = " + m + ", n = " + n);
n = 20;
System.out.println("m = " + m + ", n = " + n);//m = 10, n = 20
System.out.println("***********引用数据类型:********");
Order o1 = new Order();
o1.orderId = 1001;
Order o2 = o1; //赋值后,o1和o2的地址值相同,都指向了堆空间中同一个对象实体
System.out.println("o1.orderId = " + o1.orderId + ",o2.orderId = " + o2.orderId);
//o1.orderId = 1001,o2.orderId = 1001
o2.orderId = 1002;
System.out.println("o1.orderId = " + o1.orderId + ",o2.orderId = " + o2.orderId);
//o1.orderId = 1002,o2.orderId = 1002
}
}
class Order{
int orderId;
}
针对基本数据类型
方法的形参的传递机制:值传递
1.形参:方法定义时,声明的小括号内的参数
实参:方法调用时,实际传递给形参的数据
2.值传递机制:
如果参数是基本数据类型,此时实参赋值给形参的是实参真是存储的数据值。(其实还是遵循上面那个规则)
示例:
public class ValueTransferTest1 {
public static void main(String[] args) {
int m = 10;
int n = 20;
System.out.println("m = " + m + ", n = " + n);
//m = 10, n = 20
ValueTransferTest1 test = new ValueTransferTest1();
test.swap(m, n);
System.out.println("m = " + m + ", n = " + n);
//m = 10, n = 20
}
public void swap(int m,int n){
int temp = m;
m = n;
n = temp;
}
}
因为只是传了一个数值给局部变量,交换也只是在swap中交换,局部变量储存在栈中,换完值之后就出栈了,所以并没有换成功。
针对引用数据类型
如果参数是引用数据类型,此时实参赋值给形参的是实参存储数据的地址值。
public class ValueTransferTest2 {
public static void main(String[] args) {
Data data = new Data();
data.m = 10;
data.n = 20;
System.out.println("m = " + data.m + ", n = " + data.n);
ValueTransferTest2 test = new ValueTransferTest2();
test.swap(data);
System.out.println("m = " + data.m + ", n = " + data.n);
//m = 20, n = 10
}
public void swap(Data data){
int temp = data.m;
data.m = data.n;
data.n = temp;
}
}
class Data{
int m;
int n;
}
练习1
public class TransferTest3{
public static void main(String args[]){
TransferTest3 test=new TransferTest3();
test.first();
}
public void first(){
int i=5;
Value v=new Value();
v.i=25;
second(v,i);
System.out.println(v.i);
}
public void second(Value v,int i){
i=0;
v.i=20;
Value val=new Value();
v=val;
System.out.println(v.i+" "+i);
}
}
class Value {
int i= 15;
}
两个Value v=new Value();new出来的i:初始都是零,然后才被属性赋值,所以一开始是0然后划掉。 这种画个内存图就很好理解。
练习2
public static void method(int a,int b){
a = a * 10;
b = b * 20;
System.out.println(a);
System.out.println(b);
System.exit(0);
}
直接在方法里输出局部变量,外面的成员变量只是传了个数值进来,exit(0)是直接退出程序的意思不再向下面运行。
练习3
微软:
定义一个int型的数组:int[] arr = new int[]{12,3,3,34,56,77,432};让数组的每个位置上的值去除以首位置的元素,得到的结果,作为该位置上的新值。遍历新的数组。
for(int i = arr.length –1;i >= 0;i--){
arr[i] = arr[i] / arr[0];
}
不能正着遍历是因为在i = 0时,首个元素已经变成1并且储存进去了,所以就会出错,要么倒着遍历要么从第二个元素开始,最后再除第一个元素。还可以用一个变量代替首个元素。
int temp = arr[0];
for(int i= 0;i < arr.length;i++){
arr[i] = arr[i] / temp;
}
练习4
public class ArrayPrint {
public static void main(String[] args) {
int[] arr = new int[]{1,2,3};
//传进去的是一个Object的对象
System.out.println(arr);//地址值
char[] arr1 = new char[]{'a','b','c'};
//传进去的是一个数组,里面遍历数据了
System.out.println(arr1);//abc
}
}
我们知道println是一系列重载的方法,当输入一个整型数组时,传的是Object类型,它是万用类,它底下有个toString方法,当我们输出一个对象的引用时,实际上就是调用当前对象的toString()。
Object类中toString()的定义:
源码:
public String toString() {undefined
return getClass().getName() + “@” + Integer.toHexString(hashCode());
}
从源码中可以看到它返回的是数组的hashCode
而当你输入char类型数组时,它调的这个方法就是自动帮你遍历数组。
练习5:将对象作为参数传递给方法
/*
* 练习5:将对象作为参数传递给方法
* (1)定义一个Circle类,包含一个double型的radius属性代表圆的半径,一个findArea()方法返回圆的面积。
*
* (2)定义一个类PassObject,在类中定义一个方法printAreas(),该方法的定义如下:
* public void printAreas(Circle c,int time)
* 在printAreas方法中打印输出1到time之间的每个整数半径值,以及对应的面积。
* 例如,times为5,则输出半径1,2,3,4,5,以及对应的圆面积。
*
* (3)在main方法中调用printAreas()方法,调用完毕后输出当前半径值。
*
*/
public class Circle {
double radius; //半径
//返回圆的面积
public double findArea(){
return radius * radius * Math.PI;
}
}
定义一个PassObject类
public class PassObject {
public static void main(String[] args) {
PassObject test = new PassObject();
Circle c = new Circle();
test.printAreas(c, 5);
System.out.println("no radius is:" + c.radius);
}
public void printAreas(Circle c,int time){
System.out.println("Radius\t\tAreas");
//设置圆的半径
for(int i = 1;i <= time ;i++){
c.radius = i;
System.out.println(c.radius + "\t\t" + c.findArea());
}
//重新赋值
c.radius = time + 1;
}
}