跟着尚硅谷的 30天搞定java学了一些
项目目录
总结:
1、练习面向对象思想的基本的3步骤
基本过程
1、创建类,设计类的成员。
2、创建类的对象。
3、通过“对象.属性”或“对象.方法”调用对象的结构。
注意区别类和对象。尽量避免static关键字
2、稍微学了一点封装的思想
自己试探着创建合适的类和方法
3、注释都是我自己摸索的,包含了当时的想法和自己的思考总结,如果有问题,请留言。
Test类
public class Test {
//测试Person类
//对象基础操作,类似容器
public void testPerson1() {
//这里面定义的都是局部变量 p1 , p2
//创建Person类对象--实例化
Person p1 = new Person();
//调用对象的结构:属性、方法
//读写
p1.name = "Tom";
p1.isMale = true;
p1.age = 21;
System.out.println(p1.name);
//人类的和对象本身的
//方法和属性是属于类的还是对象的
p1.sleep();
p1.talk("普通话");
p1.eat();
//如果创建了一个类的多个对象,每个对象都有独立的
Person p2 = new Person();
System.out.println(p2.name);
//null 没有 默认初始化值
//将p1变量保存的对象地址赋值给p3
Person p3 = p1;
System.out.println(p3.name);//Tom
//new出来的是独立的,共用地址不是独立的
//引用类型(对象)都是指针
//其实所有类型都是指针-- 因为他们都是地址
}
//测试Person类,内存解析
public void testPerson2() {
Person person = new Person();
person.age = 18;
//内存解析
//在栈里面创建了age,p1对象。堆里面是一个Person对象
//首先执行初始化age操作,将2赋值给age
//然后this(p1)关键字找到对象;对象.取地址运算拿到对象的属性地址
//this->age拿到对象的地址,
//然后执行 对象地址 += age地址
//对象地址值变为 20
//return 对象地址 返回20
//然后方法结束age被回收
System.out.println(person.addAge(2));
//this.age值也改了
System.out.println(person.age);
}
//测试Circle
public void testCircle() {
Circle circle = new Circle();
circle.radius = 3;
System.out.println(circle.getArea());
}
public void testMyFun() {
MyFun myFun = new MyFun();
myFun.fun1();
System.out.println("_____________");
System.out.println("面积是" + myFun.fun2());
System.out.println("_____________");
System.out.println("面积是" + myFun.fun3(10, 20));
System.out.println("_____________");
}
public void testStudent() {
//创建对象
Test t = new Test();
// t.testMyFun();
// Student stu = new Student();
// stu.setScore();
// System.out.print(stu.score);
//
Student[] stus = new Student[20];
for(int i =0;i<20;){
//总是容易忘记创建对象
//空指针
stus[i] = new Student();
//给student[i]属性赋值
//成绩范围:[0,100]
stus[i].score = (byte)(Math.random()*(100-0+1));;
//年级范围:[1,6]
stus[i].state = (byte)(Math.random()*(6-1+1)+1);
// stus[i++].number = i;
stus[i].number = i+1;
i++;
}
int[] num = new int[10];
int i = 2;
//不同的系统可能结果不同吗? java不是跨平台吗?
// c/c++在不同系统结果不同的
// num[--i] = i;//num[1] = 1 i = 1;
// num[++i] = i;//num[3] = 3 i = 3;
// num[i++] = i;//num[2] = 3 i = 3;
// num[i--] = i;//num[2] = 1 i = 1;
// System.out.println(num[1] + " " + num[2] + " " + num[3]);
for(Student stu : stus){
System.out.println(stu);
}
System.out.println();
//按照成绩冒泡法排序 --没有说是什么顺序
for(i=0;istus[j+1].score){
Student temp = stus[j+1];
stus[j+1] = stus[j];
stus[j] = temp;
}
}
}
for(Student stu : stus){
System.out.println(stu);
}
}
public void testMyDate(String[] args) {
MyDate myDate = new MyDate();
myDate.month = 6;
myDate.year = 2022;
myDate.print();
}
}
StudentArr类
//将students数组操作封装到这个类的方法中
public class StudentArr {
//遍历Student数组的操作
public void print(Student[] stus){
for(int i=0;istus[j+1].score){
Student student = stus[j];
stus[j] = stus[i];
stus[i] = student;
}
}
if(flag){
break;
}
}
}
}
Student类
public class Student {
//学号
int number;
//年级
byte state;
//成绩
byte score;
//创建对象时怎么不一起赋值,
//对象有没有给我方法赋值,然后我调用
public void setState(){
this.state = (byte)(Math.random()*(6-1+1)+1);
}
public void setScore(){
this.score = (byte)(Math.random()*(100-0+1));
}
public String toString(){
return "编号"+number+"年级:"+state+"成绩"+score;
}
//改进,将方法封装到对象中
//遍历Student数组操作
}
Person类
//人类--抽象的类型
public 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("人可以说活");
System.out.printf("用%s语言说话n",language);
}
public void study(){
}
public void showAge(){
}
public int addAge(int age){
this.age += age;
return this.age;
}
}
MyFun类(可以定义成MyMethod)
//这个类有一些自己写的方法,
public class MyFun {
//打印10*8的*型矩形,在main方法中调用该方法
public void fun1() {
for (short i = 1; i <= 10; i++) {
for (short j = 1; j <= 8; j++) {
if (i == 1 || i == 10) {
System.out.print("*");
continue;
}
if (j == 1 || j == 8) {
System.out.print("*");
continue;
}
System.out.print(" ");
}
System.out.println();
}
}
//打印一个10*8的矩形外,再计算该矩形的面积,并将其作为方法返回值。在main方法中调用该方法,接收返回的面积值并打印。
public int fun2() {
fun1();
return 10 * 8;
}
//修改提供m和n两个参数,打印m*n矩形,计算矩形面积,作为方法返回值。
public int fun3(int m, int n) {
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
//行列打印。规则图形矩形
//思路:
//第1行,第一列,最后一行,最后一列元素都是*,其它都是空格
if (i == 1 || i == m || j == 1 || j == n) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
return m*n;
}
}
Circle类
public class Circle {
public double radius;
public double getArea() {
return this.radius * this.radius * Math.PI;
}
}
发现没有必要 --- 主要练习用普通的类(对象);3步走
Circle应该是个工具类--把它定义为final 我是类似Math
也可以是普通的对象,有半径
//public final class Circle {
//
//
利用面向对象的编程方法,设计类Circle计算圆的面积
// //常量定义
// private final double PI = Math.PI;
// //公共静态方法
// public static double getArea(double r){
// return r*r*PI;
// }
//}
MyDate类
public class MyDate {
short year;
byte month;
void print(){
System.out.printf("出生日期%d年%d月",this.year,this.month);
}
}



