JAVA面向对象
构造方法
特点
无返回值
方法名与类名相同
形式:
public 方法名(){
}
注意事项
1.如果你没写无参的构造方法
系统会给你提供一个无参的构造方法
2.如果我只写了有参的构造方法
这时系统就不会给你提供无参的构造方法
3.写类的时候 建议 无参有参构造方法全写
定义类书写顺序
成员变量 -> 无参有参构造方法 -> set/get方法 ->成员方法
代码示例1
public class Demo01{
}
class Car(){
String name;
int num;
public car(){
System.out.println("我是无参构造方法");
}
public car(String name, int num){
this.name = name;
this.num = num;
System.out.println("我是有参构造方法");
}
public Car(String name){
this.name = name;
}
public void sayHi(){
System.out.println(name + "*****" + num);
}
public void fun(){
System.out.println("哈哈");
}
}
代码示例2
需求:
创建一个矩形类 属性 长 宽
计算 周长 和 面积(都用整数就行)
要求
有参无参构造方法 set/get方法
public class Demo02{
private int side;
private int wide;
private int length;
private int area;
public Demo02(){
}
public Demo02(int side, int wide){
this.side = side;
this.wide = wide;
this.length = (side + wide) * 2;
this.area = side * wide;
}
public int getSide(){
return side;
}
public void setSide(int side){
this.side = side;
}
public int getWide(){
return wide;
}
public void setWide(int wide){
this.wide = wide;
}
public int getLength(){
return length;
}
public void setLength(int length){
this.length = length;
}
public int getArea(){
return area;
}
public void setArea(int area){
this.area = area;
}
}
静态变量 静态方法
静态变量 和 静态方法(使用static修饰)
静态变量保存在方法的静态区(共享区)
静态变量是共享的空间
静态变量如何调用
1.可以使用对象来调用
2.推荐使用类名来调用
为什么静态变量要用类名取调用
先有鸡还是先有的蛋
先有类还是先有对象
只要加载了.class文件 就加载了静态变量(这时还没有对象参数)
静态变量随着类的产生而产生
静态变量(属于类 也叫做类变量)和非静态变量(属于对象 对象变量)的区别
1.调用方式不同
静态变量:对象 或者 类名去调用(推荐 类名)
非静态变量:只能使用对象去调用
2.内存中存在的位置不同
静态变量:方法的静态区
非静态成员变量:堆区
3.加载的时机不同
静态变量:随着类的加载而加载(不依赖对象而创建)
非静态成员变量:随着对象的创建而创建(依赖对象)
4.声明周期不同
静态变量:随着类的销毁而销毁(相当于程序结束)
非静态成员变量:随着对象的销毁而销毁
什么时候设置static静态变量
当你在设计类的时候,需要考虑成员变量是否需要共享,static
修饰的方法或者变量 是被所有对象所共享的同一块内存空间
代码示例
public class Demo03{
public static void main(String[] args){
AVGirl girl1 = new AVGirl();
girl1.name = "苍老师";
AVGirl.country = "日本";
girl.sayHi();
AVGirl girl2 = new AVGirl();
girl2.name = "波多老师";
AVGirl.country = "北海道";
girl2.sayHi();
}
}
class AVGirl{
String name;
static String country;
public void sayHi(){
System.out.println("姓名为:" + name);
System.out.println("国籍为:" + country);
}
}
静态变量在内存中得加载步骤
1.加载.class文件(字节码文件)到方法区
2.main方法入栈
3.碰见AVGirl对象 先要加载AVGirl.class
AVGirl中分为静态区和非静态区 这时初始化
静态变量country的初值为null
4.创建对象(堆区开辟空间)
5.把对象地址赋值给girl1变量 保存
6.通过类名 修改类变量的值
7.sayHi方法入栈 打印
8.sayHi出栈
9.main出栈!
内存变量图形示例
静态方法与非静态方法
静态方法(使用static 修饰的方法)
1.只能使用静态变量
2.只能使用静态方法
举例:
Math.random();
Array.toString();
非静态方法:
1.可以使用静态变量和非静态变量
2.可以使用静态方法和非静态方法
代码举例
public class Demo04{
public static void main(String[] args){
Man man = new Man();
man.fun();
}
}
class Man{
int num1 = 10;
static int num2 = 5;
public void fun(){
System.out.println(num1);
System.out.println(num2);
Man.sayHi();
}
public static void sayHi(){
System.out.println("我是sayHi");
}
public static void fun1(){
System.out.println(num2);
}
}
main解析
1.main方法就是为了让jvm去调用的方法
public: jvm调用这个方法 要使用最大权限
static: jvm调用时是不需要创建对象 直接使用类名调就行(类名.main())
main: 不是关键字 jvm只认识main这个词
String[] args 接收从键盘输入的值(基本不用这个方法了)
课堂习题
五分钟内写出
1.遍历数组
2.在数组中打印最大值
3.反转数组
4.冒泡排序
这些在这篇知识前都应该会写
public void fun(int[] array){
for(int i = 0; i < array.length; i ++){
System.out.println(array[i]);
}
}
public void fun1(int[] array){
int temp = 0;
for(int i = 0;i < array.length; i++){
if(temp < array[i]){
temp = array[i];
}
}
System.out.println("最大值:" + temp);
}
public void fun2(int[] array){
for(int i = 0; i < array.length/2; i++){
int temp = array[i];
array[i] = array[array.length - 1 -i];
array[array.length - i - 1] = temp;
}
}
public void fun3(int[] array){
for(int i = 1; i < array.length; i++){
for(int j = 0; j < arrith.length - i; j++){
if(array[j] > array[j+1]){
int temp = array[j];
array[j] = array[j + 1];
array[j+1] = temp;
}
}
}
}
什么时候使用静态方法?
静态方法 只能使用静态的变量和方法
当我要写的这个方法中 没有使用到 非静态方法和变量 可以使用静态方法
当我这个类 所有方法都是静态方法
这时 这个类在使用的时候 就完全不需要对象
可以把构造方法私有化(外界就再也创建不了对象了,可以告诉使用者
使用这个类不需要创建对象) 如果写了文档注释 可以使用编译指令
生成文档
编译文档指令:
javadoc -d(指定生成文档所在的文件夹) -author -version 文件名.java
作业 创建10个对象类 并写明get/set方法
1.bear类
private static String name;
private static String color;
private static double weight;
private static double high;
public void climbTree(){
System.out.println("熊会爬树");
}
public void swim(){
System.out.println("熊会游泳");
}
public void catchFish(){
System.out.println("熊会抓鱼");
}
public void eatHoney(){
System.out.println("熊喜欢吃蜂蜜");
}
public void sayHi(){
System.out.println("姓名: " + name);
System.out.println("毛色:" + color);
System.out.println("体重:" + weight);
System.out.println("身高:" + high);
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public String getColor(){
return color;
}
public void setColor(String color){
this.color = color;
}
public double getWeight(){
return weight;
}
public void setWeight(double weight){
this.weight = weight;
}
public double getHigh(){
return high;
}
public void setGigh(double high){
this.high = high;
}
2.长方体类
private static double side;
private static double wide;
private static double high;
public static void sayHi() {
System.out.println("长方体的长为:" + side);
System.out.println("长方体的宽为:" + wide);
System.out.println("长方体的高位:" + high);
}
public static void perimeter() {
System.out.println("长方体的周长为:" + (4 * side + 4 * wide + 4 * high));
}
public static void area() {
System.out.println("长方体的面积为:" + (side * wide * 2 + side * high * 2 + wide * high * 2 ));
}
public static void volume() {
System.out.println("长方体的体积为:" + (side * wide * high));
}
public double getSide() {
return side;
}
public void setSide(double side) {
this.side = this.side;
}
public double getWide() {
return wide;
}
public void setWide(double wide) {
this.wide = wide;
}
public double getHigh() {
return high;
}
public void setHigh(double high) {
this.high = high;
}
3.青蛙类
private static String name;
private static String color;
private static String swim;
private static String type;
public static void swim(){
System.out.println("青蛙会游泳");
}
public static void catchInsets() {
System.out.println("青蛙会抓害虫");
}
public static void cry() {
System.out.println("青蛙呱呱叫");
}
public static void sayHi() {
System.out.println("姓名:" + name );
System.out.println("肤色:" + color);
System.out.println("泳技:" + swim);
System.out.println("类型:" + type);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColor() {
return color;
}
public void setColor() {
this.color = color;
}
public String getSwim(){
return color;
}
public void setSwim() {
this.swim = swim;
}
public String getType() {
return type;
}
public void setType() {
this.type = type;
}
4.树叶类
private static String name;
private static String type;
private static String color;
private static double wide;
public void fun1() {
System.out.println("树叶可以进行光合作用");
}
public void fun2() {
System.out.println("树叶可以进行氧化作用");
}
public void fun3() {
System.out.println("树叶可以成长");
}
public void sayHi() {
System.out.println("姓名:" + name);
System.out.println("类型:" + type);
System.out.println("颜色:" + color);
System.out.println("叶宽:" + wide);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public double getWide() {
return wide;
}
public void setWide(double wide) {
this.wide = wide;
}
5.导弹类
private static double length;
private static double springDiameter;
private static double weight;
public static void launch() {
System.out.println("导弹可以发射");
}
public static void fly() {
System.out.println("导弹可以飞行");
}
public static void bomb() {
System.out.println("导弹可以爆炸");
}
public static void sayHi() {
System.out.println("弹长为:" + length);
System.out.println("弹径为:" + springDiameter);
System.out.println("重量为:" + weight);
}
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
public double getSpringDiameter() {
return springDiameter;
}
public void setSpringDiameter(double springDiameter) {
this.springDiameter = springDiameter;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
6.猴子类
private static String name;
private static String color;
private static String sex;
private static String type;
public static void sayHi() {
System.out.println("姓名:" + name);
System.out.println("颜色:" + color);
System.out.println("性别:" + sex);
System.out.println("类型:" + type);
}
public static void climb() {
System.out.println("猴子 会 爬树");
}
public static void eat() {
System.out.println("猴子 会 吃饭");
}
public static void sleep() {
System.out.println("猴子 会 睡觉");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColor() {
return color;
}
public void setColor() {
this.color = color;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getType() {
return type;
}
public void setType() {
this.type = type;
}
7.飞机类
private static double weight;
private static double speed;
private static int power;
private static double ammunition;
public void fly(){
System.out.println("飞机可以快速飞行");
}
public void manned(){
System.out.println("飞机可以载人");
}
public void bomb(){
System.out.println("飞机可以投弹");
}
public void sayHi(){
System.out.println("起飞重量为:" + weight);
System.out.println("飞行速度为:" + speed);
System.out.println("发动功率为:" + power);
System.out.println("挂弹数量为:" + ammunition);
}
public double getWeight(){
return weight;
}
public void setWeight(double weight){
this.weight = weight;
}
public double getSpeed(){
return speed;
}
public void setSpeed(double speed){
this.speed = speed;
}
public int getPower(){
return power;
}
public void setPower(int power){
this.power = power;
}
public double getAmmunition(){
return ammunition;
}
public void setAmmunition(double ammunition){
this.ammunition = ammunition;
}
8.坦克类
private static String model;
private static double range;
private static double weight;
private static double speed;
public void run(){
System.out.println("坦克可以跑");
}
public void artillery(){
System.out.println("坦克可以发射炮弹");
}
public void turn(){
System.out.println("坦克可以转弯");
}
public void sayHi(){
System.out.println("型号为:" + model);
System.out.println("射程为:" + range);
System.out.println("重量为:" + weight);
System.out.println("速度为:" + speed);
}
public String getModel(){
return model;
}
public void setModel(String model){
this.model = model;
}
public double getRange(){
return range;
}
public void setRange(double range){
this.range = range;
}
public double getWeight(){
return weight;
}
public void setWeught(double weight){
this.weight = weight;
}
public double getSpeed(){
return speed;
}
public void setSpeed(double speed){
this.speed = speed;
}
9.大象类
private static String name;
private static double high;
private static double weight;
private static String color;
public void sleep() {
System.out.println("老虎会睡觉");
}
public void eat() {
System.out.println("老虎会捕食");
}
public void breath() {
System.out.println("老虎会呼吸");
}
public void sayHi() {
System.out.println("姓名:" + name);
System.out.println("身高:" + high);
System.out.println("体重:" + weight);
System.out.println("颜色:" + color);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getHigh() {
return high;
}
public void setHigh(double high) {
this.high = high;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
10.鲸鱼类
private static double weight;
private static double high;
private static String type;
private static int age;
public void swim() {
System.out.println("鲸鱼 会 游泳");
}
public void waterSpray() {
System.out.println("鲸鱼 会 喷水");
}
public void eat() {
System.out.println("鲸鱼会捕食");
}
public double getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public double getHigh() {
return high;
}
public void setHigh(double high) {
this.high = high;
}
public String getType() {
return type;
}
public void setType() {
this.type = type;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}