Java面向对象(1)

Java面向对象

面向对象思想编程内容的三条主线:

1、Java类及类成员:属性、方法、构造器;代码块、内部类
2、面向对象的三大特征:封装性、继承性、多态性、(抽象性)
3、其他关键字:this、super、static、final、abstract、interface、package、import
面向对象,将功能封装进行对象,强调具备了功能的对象,以类/对象为最小单位,考虑谁来做。
static、fianl、abstract 修饰方法;
4种权限修饰符:private、public、缺省、protected;


虚拟机栈,即为平时提到的栈结构。我们将局部变量存储在栈结构中
堆:将new出来的结构(比如:数组,对象)加载在堆空间中。补充:对象的属性(非static的)加载在堆空间中。
方法区:类的加载信息、常量池、静态域


属性:根据其类型,都默认初始化值。引用数据类型(类、数组、接口:null)
局部变量:没有默认初始值。意味着,调用局部变量之前,一定要显示赋值
属性加载到堆空间中(非static),局部变量加载到栈中

栗子
//计算圆的面积
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.Area();
	}
}
class Circle{
	double radius;
	
	public double findArea() {
		double area= Math.PI * radius * radius;
		return area;
	}
	
	public void Area() {
		double area = Math.PI * radius * radius;
		System.out.println("面积为:" + area);
	}
}
调用方法的方法
public class test01 {
	public static void main(String[] args) {
		test01 o1=new test01();//构造对象
		o1.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 StudentTest {
	public static void main(String[] args) {
		//声明student类型数组
		Student[] stu = new Student[20];
		for(int i=0;i<stu.length;i++) {
			stu[i]=new Student();
			//给数组元素对象赋值
			stu[i].number=i+1;
			//年级:[1,6]
			stu[i].state=(int)(Math.random()*(6-1+1)+1);
			//成绩:[0,100]
			stu[i].score=(int)(Math.random()*(100-0+1));
		}
		//遍历学生数组
		for(int i=0;i<stu.length;i++) {
			stu[i].show();
		}
		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++) {
			stu[i].show();
		}
	}
}
class Student{
	int number;//学号
	int state;//年级
	int score;//成绩
	
	public void show() {
		System.out.println("学号:" + number + ";年级:" +state + ";成绩:"+score);
	}
}
改进(将main函数中方法进行封装)
public class StudentTest {
	public static void main(String[] args) {
		//声明student类型数组
		Student[] stu = new Student[20];
		for(int i=0;i<stu.length;i++) {
			stu[i]=new Student();
			//给数组元素对象赋值
			stu[i].number=i+1;
			//年级:[1,6]
			stu[i].state=(int)(Math.random()*(6-1+1)+1);
			//成绩:[0,100]
			stu[i].score=(int)(Math.random()*(100-0+1));
		}
		
		//遍历学生数组
		StudentTest te = new StudentTest();//这个方式非常重要
		te.print(stu);
		
		System.out.println("********");
		
		te.sort(stu);
		te.print(stu);
	}
	public void print(Student[] stu) {
		for(int i=0;i<stu.length;i++) {
			stu[i].show();
		}
	}
	//使用冒泡排序按学生成绩进行排序
	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 void show() {
		System.out.println("学号:" + number +
				 ";年级:" +state + ";成绩:"+score);
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值