Java面向对象

本文详细介绍了Java面向对象编程的核心概念,包括类与对象的定义、面向过程与面向对象的区别、类和对象的使用、属性与方法、内存解析以及实践中的类设计和方法调用示例。
摘要由CSDN通过智能技术生成

 一、Java面向对象学习的三条主线:

1、Java类及类的成员:属性、方法、构造器、代码块、内部类

2、面向对象的三大特征:封装性、继承性、多态性、(抽象性)

3、其他关键字:this、super、static、final、abstract、interface、package、import等

二、面向过程与面向对象的区别(人把象装进冰箱)

1、面向过程:强调的是功能行为、以函数为最小单位,考虑怎么做。

①把冰箱门打开

②抬起大象、塞进冰箱

③吧冰箱门关闭

2、面向对象:强调具备了功能的对象,以类/对象为最小单位,考虑谁来做。

人{

    打开(冰箱){

                冰箱.开开()

        }

    关闭(冰箱){

                冰箱.闭合()

        }

        抬起(大象){

                大象.进入(冰箱)

        }

}

冰箱{

    开开(){}

    闭合(){}

}

大象{

    进入(冰箱){

    }

}

三、面向对象的两个要素

类:对一类事物的描述,是抽象的、概念上的定义

对象:是实际存在的该类事物的每个个体,因而也称为实例(instance)

>面向对象程序的重点是类的设计

>设计类,就是设计类的成员

    ①属性=成员变量=field=域、字段                  

       方法=成员方法=函数=method

       创建类的对象=类的实例化=实例化类

三、类和对象的使用(面向对象思想落地的实现)

1、创建类、设计类的成员

2、创建类的对象

3、通过“对象.属性”或“对象. 方法”调用对象的结构

四、如果创建了一个类的多个对象,则每个对象都独立的拥有一套类的属性。(非static的)

意味:如果我们修改了一个对象的属性a,则不影响另外一个对象属性a的值。

五、对象的内存解析

六、类中属性的使用

属性(成员变量)   vs      局部变量

1、相同点:

            ①定义变量的格式:数据类型 变量名 = 变量值

            ②先声明后使用

            ③变量都有其对应的作用域

2、不同点:

            ①在类中申明的位置的不同

                属性:直接定义在类的一对{}内

                局部变量:声明方法内、方法形参、代码块内、构造器形参、构造器内部的变量。

            ②关于权限修饰符的不同

                属性:可以在声明属性时,指明其权限、使用权限修饰符

                常用的权限修饰符:private、public、缺省。protected    ---->封装性

                局部变量:不可以使用权限修饰符

            ③默认初始化值的情况

                属性:类的属性,根据其类型,都有默认初始化值。

                        整型(byte、short、int、long):0

                        浮点型(float、double):0.0

                        字符型(char):0(或‘\u0000’)

                        布尔型(boolean):false

                        引用数据类型(类、数组、接口):null

    

                局部变量:没有默认初始化值

                (意味着在调用局部变量之前,一定要显示赋值;特别的,形参在调用时,赋值即可)

                ④在内存中加载的位置:

                        属性:加载到堆空间中  (非static)

                        局部变量:加载到栈空间

七、类中方法的声明和使用

方法:描述类应该具有的功能。

比如:Math类:sqrt()\random()\...

          Scanner类:nextXxx()...

          Arrays类:sort()\binarySearch()\toString()\equals()\...

1、方法的声明:权限修饰符 返回值类型 方法名(形参列表){

                                                    方法体

                            }

        注意:static、final、abstract来修饰的方法在后面

2、说明:

        ①关于权限修饰符:

                Java规定的四种权限修饰符:private、public、缺省、protected-->封装性再细说。

        ②返回值类型:有返回值  VS  无返回值

                    》如果方法有返回值、则必须再方法声明时,指定返回的类型。同时,方法中需要使用return关键字来返回指定类型的变量或常量。

                       如果方法没有返回值,则方法声明时,使用void来表示。通常,没有返回值的方法中,就不适用return。但是,如果使用的话,只能“return;”,结束此方法的意思。

                    》定义方法该不该有返回值?

                        按照题目要求;根据经验

        ③方法名:属于标识符,遵循标识符的规则和规范,“见名知意”

        ④形参列表:方法可以声明0个,1个,或多个形参。

                    》格式:数据类型1  形参1,数据类型2  形参2,...

                    》定义方法该不该定义形参?

                        按照题目要求;根据经验

        ⑤方法体:方法功能的体现

3、return关键字的使用:

        ①使用范围:使用再方法体中

        ②作用:结束方法;针对于有返回值类型的方法,使用“return  数据”方法返回所需的数据

        ③注意:return关键字后面不可以声明执行语句。

4、方法的使用:可以调用当前类的属性和方法 

                        特殊的:方法A中又调用了方法A:递归方法

        方法中不可以再次定义另一个方法

练习一:创建一个Person类

要求:(1)创建Person类的对象,设置该对象的name、

age和sex属性,调用study方法,输出字符串

“studying” ,调用showAge()方法显示age值,调用

addAge()方法给对象的age属性值增加2岁。

(2)创建第二个对象,执行上述操作,体会同一个类的

不同对象之间的关系。

package object;

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;
	}
}

package object;

public class personText {
	public static void main(String[] args) {
		person p1=new person();
		
		p1.name="min";
		p1.age=100;
		p1.sex=0;
		
		p1.study();
		
		p1.showAge();
		
		int newAge=p1.addage(2);
		System.out.println(p1.name+"的新年龄为:"+newAge);
		System.out.println(p1.age);
	}
}

练习2:利用面向对象的编程方法,设计类Circle计算圆的面积

package object;
import java.util.Scanner;
public class CircleTest {
	public static void main(String[] args) {
		Circle c1=new Circle();
		Scanner scanner=new Scanner(System.in);  // 快捷方式:ctrl+shift+o
		System.out.println("请输入半径(cm):");
		c1.radius=scanner.nextDouble();
		double area = c1.findArea();
		System.out.println(area);
	}

}
class Circle{
	double radius;//把半径作为一个属性,更为合适
	
	public double findArea(){
		double area = Math.PI*radius*radius;
		return area;
	}
	
	//错误情况
	//public double findArea(double r){
		//double area = Math.PI*r*r;
		//return area;
	//}
}

练习三、

 编写程序,声明一个method方法,在method方法提供m和n两个参数,方法中打印一个m*n的*型矩形,并计算该矩形的面积, 将其作为方法返回值。在main方法中调用该方法,接收返回的面积值并打印。

package object;
import java.util.Scanner;
public class rectangle {
	public static void main(String[] args) {
		method m1=new method();
		Scanner scanner=new Scanner(System.in);  // 快捷方式:ctrl+shift+o
		System.out.println("请输入矩形的长:");
		int x = scanner.nextInt();  // 快捷方式:ctrl+1
		System.out.println("请输入矩形的宽:");
		int y = scanner.nextInt();  // 快捷方式:ctrl+1
		m1.picture(x,y);
		int area=m1.findarea(x,y);
		System.out.println("矩形的面积值为:"+area);
	}

}

class method{
	public void picture(int m,int n){
		for(int i=0;i<m;i++){
			for(int j=0;j<n;j++){
				System.out.print("*  ");
			}
			System.out.println();
		}
	}
	public int findarea(int m,int n){
		int area=m*n;
		return area;
	}
}

练习四

定义类Student,包含三个属性:学号number(int),年级state(int),成绩

score(int)。 创建20个学生对象,学号为1到20,年级和成绩都由随机数确定。

问题一:打印出3年级(state值为3)的学生信息。

问题二:使用冒泡排序按学生成绩排序,并遍历所有学生信息

提示:

1) 生成随机数:Math.random(),返回值类型double;

2) 四舍五入取整:Math.round(double d),返回值类型long。

package object;
public class grade {
	public static void main(String[] args) {
		Student[] stud=new Student[20];    //声明Student类型的数组
		for(int i=0;i<stud.length;i++){
			stud[i]=new Student();      //给数组元素赋值
			
			//给Student对象的属性赋值
			stud[i].number=i+1;     
			//年级[1,6]   Math.random函数随机生成[0.0,1.0)中的一个double型数,乘以(b-a+1)得到[a,b),再加1得到[(a+1).0,(b+1).0),
			//之后强制转换成int型,最后得到区间[a+1,b]
			stud[i].state=(int)(Math.random()*(6-1+1)+1);
			stud[i].score=(int)(Math.random()*(100-0+1));
			if(stud[i].state==3)
				stud[i].show();
			System.out.println();
		}
		for(int i=0;i<stud.length;i++){
			stud[i].show();
		}
		System.out.println();
		for(int i=0;i<stud.length-1;i++){
			for(int j=0;j<stud.length-i-1;j++){
				if(stud[j].score>stud[j+1].score){
					//如果需要换顺序,交换的是数组的元素:Student对象!!!
					Student temp=stud[j];
					stud[j]=stud[j+1];
					stud[j+1]=temp;
				}
			}
		}
		System.out.println();
		for(int i=0;i<stud.length;i++){
			stud[i].show();
		}
	}

}
class Student{
	int number;
	int state;
	int score;
	
	public void show(){
		System.out.println("学号"+number+"年级"+state+"成绩"+score); 
	}
}

把数组遍历、排序、查询封装成方法

package object;
public class grade1 {
	public static void main(String[] args) {
		Student1[] stud=new Student1[20];    //声明Student类型的数组
		grade1 test=new grade1();
		for(int i=0;i<stud.length;i++){
			stud[i]=new Student1();      //给数组元素赋值
			
			//给Student对象的属性赋值
			stud[i].number=i+1;     
			//年级[1,6]   Math.random函数随机生成[0.0,1.0)中的一个double型数,乘以(b-a+1)得到[a,b),再加1得到[(a+1).0,(b+1).0),
			//之后强制转换成int型,最后得到区间[a+1,b]
			stud[i].state=(int)(Math.random()*(6-1+1)+1);
			stud[i].score=(int)(Math.random()*(100-0+1));
		}

		test.print(stud);
		System.out.println("************************************************");
		test.sort(stud);
		System.out.println("************************************************");
		test.print(stud);
		System.out.println("************************************************");
		test.searchstate(stud,3);
	}

	/**
	 * 
	 * @Description  遍历数组的操作封装到方法里
	 * @author min
	 * @date 2024年4月2日下午3:28:48
	 * @param stud
	 */
	public void print(Student1[] stud){
		for(int i=0;i<stud.length;i++)
			stud[i].show();
	}
	/**
	 * 
	 * @Description  查找Student数组中指定年级的学生信息
	 * @author min
	 * @date 2024年4月2日上午11:34:59
	 * @param stud   要查找的数组
	 * @param state  要查找的年级
	 */
	public void searchstate(Student1[] stud,int state){
		for(int i=0;i<stud.length;i++){
			if(stud[i].state==state)
				stud[i].show();
		}
	}
	/**
	 * 
	 * @Description  给数组排序
	 * @author min
	 * @date 2024年4月2日下午3:27:57
	 * @param stud
	 */
	public void sort(Student1[] stud){
		for(int i=0;i<stud.length-1;i++){
			for(int j=0;j<stud.length-i-1;j++){
				if(stud[j].score>stud[j+1].score){
					//如果需要换顺序,交换的是数组的元素:Student对象!!!
					Student1 temp=stud[j];
					stud[j]=stud[j+1];
					stud[j+1]=temp;
				}
			}
		}
	}
}
class Student1{
	int number;
	int state;
	int score;
	
	public void show(){
		System.out.println("学号"+number+"年级"+state+"成绩"+score); 
	}
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值