java类的成员、属性、方法、构造器;代码块、内部块
面向对象的三大特征:封装、继承、多态
关键字:this,super,static,final,abstract,interface,package,import
创建对象,包含方法属性
class DuiXiang{ public static void main(String[] args) { Student student = new Student(); student.rest(); student.name = "Mark"; student.talk("weishnem"); } } class Student{ //属性 int old = 1; String name; int number; //方法 public void study() { System.out.println("学生在学习"); } public void rest() { System.out.println("学生在休息"); } public void talk(String s) { System.out.println(s); } }
对象的内存解析
class DuiXiang{ public static void main(String[] args) { Student s1 = new Student(); s1.name = "mark"; Student s2 = new Student(); System.out.println(s2.number); Student s3 = s2; s3.old = 33; } } class Student{ //属性 int old = 1; String name; int number; //方法 public void study() { System.out.println("学生在学习"); } public void rest() { System.out.println("学生在休息"); } public void talk(String s) { System.out.println(s); } }
以上代码的内存解析:
注意,这里s1,s2是同一地址
JVM内存内部结构
相同点:
声明格式: 数据类型 变量名 = 变量值
先声明,后使用
变量都有其对应的作用域
不同点:
属性:直接定义的类的一对{}
局部变量:声明在方法内、方法形参、代码块内、构造器形参、构造器内部的变量
关于修饰符:
可以在声明属性,指明其权限,使用权限修饰符
常用的权限修饰符:private,public、缺省、protect
局部变量:不可以使用权限修饰符
(1)类的构造
在com.atguigu.exer下的People类:
People.java:
package com.atguigu.exer; public class People { String name; public int age; int sex; public void study() { System.out.println("im studing"); } public void showAge() { System.out.println("age:" + this.age); } public int addAge(int i) { age += i; return age; } }
在其他包下:
import com.atguigu.exer.People; class DuiXiang{ public static void main(String[] args) { People p = new People(); p.study(); p.addAge(5); p.age = 4; p.showAge(); p.addAge(5); p.showAge(); } }
结果:
(2)题目:圆的面积求和
文件目录:
package com.atguigu.exer; public class CircleCul { double d;//直径 double s;//面积 double c;//周长 public double cul() { this.s = Math.PI * (this.d/2) * (this.d/2); return s; } }
Test.java
package com.atguigu.exer; public class Text { public static void main(String[] args) { CircleCul yuan = new CircleCul(); yuan.d = 7; System.out.println(yuan.cul()); } }
(3)方法
package com.atguigu.exer; public class Text { public static void main(String[] args) { Text t = new Text(); int area = t.mathod(4,5); System.out.println(area); } public int mathod(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)
package com.atguigu.exer; public class Text { public static void main(String[] args) { Student[] sArray = new Student[20]; for(int i = 0;i < sArray.length;i++) { sArray[i] = new Student(); sArray[i].number = i+1; sArray[i].state = (int)(Math.round((Math.random()*2 + 1))); sArray[i].score = (int)(Math.round((Math.random()*100))); } Text text = new Text(); text.searchThreeState(sArray); text.sortScore(sArray); text.showStudent(sArray); } /* * @用于找到三年级学生 */ public void searchThreeState(Student[] sArray) { System.out.println("输出三年级学生信息"); for(int i = 0;i < sArray.length;i++) { if(sArray[i].state == 3) { System.out.printf("学生学号为%d,学生成绩为%d",sArray[i].number,sArray[i].score); System.out.println(); } } } /* * @冒泡排序 */ public void sortScore(Student[] sArray) { System.out.println("冒泡排序"); for(int i = 0;i < sArray.length-1;i++) { for(int j = 0;j < sArray.length-i-1;j++) { if(sArray[j].score < sArray[j+1].score) { Student temp = new Student(); temp = sArray[j]; sArray[j] = sArray[j+1]; sArray[j+1] = temp; } } } } /* * @打印到操作台 */ public void showStudent(Student[] sArray) { for(int i = 0;i < sArray.length;i++) { System.out.printf("学号:%d,成绩:%d,年级:%d\n",sArray[i].number,sArray[i].score,sArray[i].state); } } } class Student{ int number;//学号 int state;//年级 int score;//分数 }
输出
(5)对象数组的内存解析
package com.atguigu.exer; public class Text { public static void main(String[] args) { Student[] stus = new Student[5]; stus[0] = new Student(); //sysout(stus[0].state);//1 //sysout(stus[1])//null //sysout(stus[1].number)//空指针异常 } } class Student{ int number;//学号 int score;//分数 int state=1;//年级 }