1、方法
package com.wyq.study;
public class Phone {//【1】创建类名
//【2】创建属性
String brand;
double price;
//【3】编写方法,方法中参数可以自己定义
public void call(String name){
System.out.println("使用"+brand+"牌子的手机在给"+name+"打电话。");
}
public Phone(){
}
public Phone(String brand){
System.out.println("使用"+brand+"牌子的手机。");
}
public Phone(String brand, double price) {
brand = brand;//也可以将this省略不写
this.brand = brand;
this.price = price;
System.out.println("我买了一部"+brand+"牌子的手机,价格是"+price+"元");
}
}
2、调用
package com.wyq.study;
public class TestPhone {
//主方法
public static void main(String[] args) {
//创建对象 类名 对象名 = new 类名();
Phone p = null; //在栈内存中声明
p = new Phone(); //在堆内存中开空间
//赋值 对象名.属性名 = 值
p.brand = "华为";
p.price = 100;
//方法的调用 对象名.方法名(),传递参数
p.call("张三");
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
Phone p2= new Phone();
p2.brand = "苹果";
p2.price = 19999;
p2.call("李四");
System.out.println("_________________________________________");
p.brand = p2.brand;
p.call("王五");
System.out.println("+++++++++++++++++++++++++++++++++++++++++");
Phone ph1 = new Phone("小米",233.33);
}
}
方法的调用:
1、书写方法
1)书写方法名
2)书写属性
3)书写方法
2、方法调用
1)创建main方法
2)创建对象
3)给属性赋值
4)进行调用