原文地址:http://blog.csdn.net/liurongsheng123/article/details/79017157
JAVA面向对象
构造方法
特色
无返回值
方法名与类名相同
形式:public 方法名(){
}1234567
注意事项
1.若是你没写无参的构造方法
系统会给你提供一个无参的构造方法
2.若是我只写了有参的构造方法
这时系统就不会给你提供无参的构造方法
3.写类的时候 建议 无参有参构造方法全写12345
定义类书写顺序
成员变量 -> 无参有参构造方法 -> set/get方法 ->成员方法1
代码示例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("哈哈");
}
}123456789101112131415161718192021222324252627282930313233
代码示例2
需求:
建立一个矩形类 属性 长 宽
计算 周长 和 面积(都用整数就行)
要求
有参无参构造方法 set/get方法12345
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;
} //建议不要修改set get方法的写法
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;
}
}1234567891011121314151617181920212223242526272829303132333435363738394041424344
静态变量 静态方法
静态变量 和 静态方法(使用static修饰)
静态变量保存在方法的静态区(共享区)
静态变量是共享的空间123
静态变量如何调用
1.可使用对象来调用
2.推荐使用类名来调用12
为何静态变量要用类名取调用
先有鸡仍是先有的蛋
先有类仍是先有对象
只要加载了.class文件 就加载了静态变量(这时尚未对象参数)
静态变量随着类的产生而产生12345
静态变量(属于类 也叫作类变量)和非静态变量(属于对象 对象变量)的区别
1.调用方式不一样
静态变量:对象 或者 类名去调用(推荐 类名)
非静态变量:只能使用对象去调用
2.内存中存在的位置不一样
静态变量:方法的静态区
非静态成员变量:堆区
3.加载的时机不一样
静态变量:随着类的加载而加载(不依赖对象而建立)
非静态成员变量:随着对象的建立而建立(依赖对象)
4.声明周期不一样
静态变量:随着类的销毁而销毁(至关于程序结束)
非静态成员变量:随着对象的销毁而销毁123456789101112
何时设置static静态变量
当你在设计类的时候,须要考虑成员变量是否须要共享,static 修饰的方法或者变量 是被全部对象所共享的同一块内存空间12
代码示例
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);
}
}12345678910111213141516171819202122
静态变量在内存中得加载步骤
1.加载.class文件(字节码文件)到方法区2.main方法入栈3.遇见AVGirl对象 先要加载AVGirl.class AVGirl中分为静态区和非静态区 这时初始化
静态变量country的初值为null4.建立对象(堆区开辟空间)5.把对象地址赋值给girl1变量 保存6.经过类名 修改类变量的值7.sayHi方法入栈 打印8.sayHi出栈9.main出栈!1234567891011
内存变量图形示例


静态方法与非静态方法
静态方法(使用static 修饰的方法)1.只能使用静态变量2.只能使用静态方法
举例:Math.random();Array.toString();
非静态方法:1.可使用静态变量和非静态变量2.可使用静态方法和非静态方法12345678910
代码举例
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(num1); 静态方法没法调用非静态变量
System.out.println(num2);
}
}1234567891011121314151617181920212223242526272829
main解析
1.main方法就是为了让jvm去调用的方法public: jvm调用这个方法 要使用最大权限static: jvm调用时是不须要建立对象 直接使用类名调就行(类名.main())main: 不是关键字 jvm只认识main这个词String[] args 接收从键盘输入的值(基本不用这个方法了)12345
课堂习题
五分钟内写出
1.遍历数组
2.在数组中打印最大值
3.反转数组
4.冒泡排序
这些在这篇知识前都应该会写123456
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;
}
}
}
}1234567891011121314151617181920212223242526272829303132333435
何时使用静态方法?
静态方法 只能使用静态的变量和方法
当我要写的这个方法中 没有使用到 非静态方法和变量 可使用静态方法12
当我这个类 全部方法都是静态方法
这时 这个类在使用的时候 就彻底不须要对象
能够把构造方法私有化(外界就再也建立不了对象了,能够告诉使用者
使用这个类不须要建立对象) 若是写了文档注释 可使用编译指令
生成文档
编译文档指令:
javadoc -d(指定生成文档所在的文件夹) -author -version 文件名.java1234567
做业 建立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;
}1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
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;
}123456789101112131415161718192021222324252627282930313233343536373839
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;
}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
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;
}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
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;
}123456789101112131415161718192021222324252627282930313233343536373839
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;
}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
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;
}12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
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;
}1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
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;
}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
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;
}