一、类与对象
类:表示客观世界中某类群体的一些基本特征抽象,即抽象的概念集合。
对象:表示具体的事物,可以使用的事物,一个个独立的个体,也称实例(instance)。
二、类的结构
1、类由属性(对应类中的成员变量)和 方法(对应类中的成员方法)构成。
2、类和对象的创建:
如果一个类创建了多个对象,每个对象拥有独自的类的属性。
问:为什么在Person类定义的methods方法没有加上static?
在主类中定义,并且由主方法直接调用的方法必须加static,而Person类定义的methods方法是由对象调用的。
public class class_field {
public static void main(String[] args){
//创建Person类的对象
Person p1 = new Person();
//调用属性:对象名.属性
p1.name = "billkin";
p1.sex = "male";
p1.age = 22;
//调用方法:对象名.方法
p1.methods();
}
}
//创建Person类
class Person{
String name;
int age;
String sex;
public void methods(){
System.out.println("姓名是:" + name + ",年纪是:" + age + ",性别是" + sex);
}
}
三、内存空间解析
p1和p2分别指向不同的栈内存,栈内存中保存着对象的地址信息(即堆空间),将p1保存的对象地址赋给p2,此时p1和p2指向同一块堆内存空间,所以p3修改属性时会改变p1对象的内容。
一块堆内存可以同时被多个栈内存共同指向,而一块栈内存只能保存一块栈内存空间的地址。
Person p1 = new Person();
Person p2 = new Person();
p1.name = "marry";
p1.sex = "male";
p1.age = 22;
System.out.println(p1.name); //marry
p2 = p1;
p2.name = "Mike";
System.out.println(p1.name); //Mike
四、属性(成员变量)与局部变量的区分
相同点:
(1)定义变量的格式:数据类型 变量名 = 变量值;
(2)先声明,后使用;
(3)都有使用的作用域
不同点:
(1)在类中声明的位置不同:
属性定义在类的{}中;
局部变量声明在方法内,方法形参,代码块,构造器形参,构造器内部变量
(2)权限修饰符不同
属性:可以在声明属性时,指明其权限,使用权限修饰符,常用的权限修饰符:private、public、缺省(默认)、protected
局部变量:不可以使用权限修饰符
(3)默认初始化值
属性:根据其类型,都有其不同的初始化值
整型(int、short、long、byte): 0
浮点型(float、double): 0.0
字符型(char) :0
Boolean:false
引用数据类型(数组、类、接口):null
局部变量:没有初始化值
(4)在内存中加载的位置是不一样的
属性:加载到堆内存(非static)
局部变量:加载到栈内存
五、类中方法的声明和使用
六、练习题
1、利用面向对象的编程方法,设计类Circle计算圆的面积
//设计类求圆的面积
public class circleArea {
public static void main(String[] args){
Circle c1 = new Circle();
c1.radius = 4.1;
// 方式一的计算
// double area = c1.circleArea();
// System.out.println(area);
// 方式二的计算
c1.circleArea();
}
}
class Circle{
double radius;
// 方式一:有返回值的方法
// public double circleArea(){
// double area = Math.PI * radius * radius;
// return area;
// 方式二: 无返回值的方法
public void circleArea(){
double area = Math.PI * radius * radius;
System.out.println(area);
}
}
2、
(1)编写程序,声明一个method方法,在方法中打印一个10*8 的*型矩形, 在main方法中调用该方法。
public class exe03 {
public static void main(String[] args){
exe03 test = new exe03();
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 exe03 {
public static void main(String[] args){
exe03 test = new exe03();
int area = test.method(); //接受面积值并且打印
System.out.println("面积是:" + area);
}
public int method(){ //设置返回值回int
for (int i = 0; i < 10 ; i++){
for (int j = 0; j < 8 ;j++){
System.out.print("* ");
}
System.out.println();
}
return 10*8; //返回一个int值
}
}
public class exe03 {
public static void main(String[] args){
exe03 test = new exe03();
int area = test.method(8,9);
System.out.println("面积是:" + area);
}
public int method(int m, int n){ //定义形参:接受m和n的值
for (int i = 0; i < m ; i++){
for (int j = 0; j < n ;j++){
System.out.print("* ");
}
System.out.println();
}
return m*n;
}
}
3、
对象数组题目:
public class exe04 {
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)+0);
}
exe04 test = new exe04();
//遍历
test.print(stu);
//查找指定state
test.searchState(stu,3);
//排序
test.sort(stu);
test.print(stu);
}
/**
* 遍历学生数组的封装
* @param stu
*/
public void print(Student[] stu){
for (int j = 0; j<stu.length; j++){
System.out.println(stu[j].info());
}
System.out.println("----------------------");
}
/**
*查找指定state的学生信息的封装
* @param stu
* @param state
*/
public void searchState(Student[] stu, int state){
for(int i = 0; i<stu.length; i++){
if (stu[i].state == state){
System.out.println(stu[i].info());
}
}
System.out.println("-------------------");
}
/**
* 冒泡排序的封装
* @param stu
*/
public void sort(Student[] stu){
for (int j = 0; j<stu.length-1; j++){
for (int i = 0; i<stu.length-1-i; i++){
if (stu[j].score > stu[j+1].score){
//定义变量保存学生对象,交换的是对象
Student temp = stu[j];
stu[j] = stu[j+1];
stu[j+1] = temp;
}
}
}
System.out.println("---------------------");
}
}
/**
* 定义Student类
*/
class Student{
int number;
int state;
int score;
public String info(){
return "学号为:" + number + ",年级为:" + state + ",分数为:" + score;
}
}