何李娇 2016-1-4
笔记:
public class Car{
//
public String name;
public String color;
public double price;
public double weight;
//speed:速度
//int speed 表示的是形式参数(形参),是方法定义时的参数
public void drive(int speed){
System.out.println("以每小时"+speed+"公里的速度行驶");
}
}
public static void main(String[] args){
//声明并且构建出类的对象
Car car = new Car();
//给对象的属性赋值
car.name = "兰博基尼";
car.color = "red";
car.price = 336.8;
car.weight = 5.6;
//通过[对象名.属性名]来调用类中的属性
System.out.println(car.name);
System.out.println(car.color);
System.out.println(car.price);
System.out.println(weight);
//实际参数(实参):方法调用时传递的参数
//car.drive|(speed);这样写是不对的,我们必须要传递一个真实的相同数
car.drive(new Random().nextInt(360)+1);
//The method println(boolean) in the type PrintStrean
//is nor applicable for the arguments(void)
//System.out.println(car.drive(125));
//快捷键:Ctrl+Shift=F:格式化代码
}
//方法的重载:
//在同一个类当中,方法名相同,参数不同(参数类型不同,参数个数不同)叫做方法重载
//方法的重载与方法的返回值类型无关.
public class Calculate{
//方法调用时传递的参数要与方法定义时的参数类型保持一致.
//方法定义的返回值类型与方法返回的数据类型与接收方法调用后的数据类型保持一致
public int add(int num1,int num2){
int tesult= num1 + num2;
//return:在return关键字的后面不能有任何的可执行代码(返回值)
return result;
}
public int add(int num1,int num2,int num3){
int result = num1+num2+num3;
return result;
}
public int subtract(int num1,int num2){
int result = num1 - num2;
return result;
}
public double add(double num1, double num2){
double result = num1 + num2;
return result;
}
}
public class TestCalculate{
public static void main(String[] args){
Calculate cal = new Calculate();
int resule = cal.add(3,5);
System.out.println(result);
result = cal.subtract(5,2);
System.out.println(result);
result = cal.add(7,8,9);
System.out.println(resulr);
double result2 = cal.add(3.14,0.618);
System.out.println(result2);
}
}
/*
一个对象在不同的时候可以拥有多个引用,但是在同一个时刻一个引用只能够
指向一个对象,如果有一个引用对对象做了更改,那么将影响到其它的引用.
在同一个类中构建出来的每个对象都各自拥有一套自己的属性,
互相之间不受影响(在讲解在static关键字之前都可以这么理解);
每个构建出来的对象都各自拥有一个自己的属性,但是类中的方法是共有的,储存在方法区中.*/
//参数的传递,本质上就是值得传送
public class People{
public int age = 15;
public void change(People p){
p.age = 25;
}
public void change2(Peoplep){
}
}
public class Test{
String str = new String("good");
char[] arr = {'a','b','c'};
public static void main(String[] args){
Test test = new Test();
test.change(test.str,test.arr);
System.out.print(test.str+"and");
System.out.print(test.arr);
}
public void change(String str,char[] arr){
str = "test ok";
arr[0] = 'g';
}遇到这个大括号,则str的作用域结束
good and gbc
String pool
str
new
String("good");
test ok
str
arr
a b cg
arr
Stack Heap
}
/**
* 限定范围修饰词:
* public 共有的 整个工程公用 安全性最低
* default 默认的(缺省的) 本包之内可用
* protected 受保护的 本包之内可以用
* private 私有的 只在本类可用 安全性最高
*/
public class DemoA{
public int num = 5;
public void sing(){
System.out.println("singsing the ABC song");
}
public static void main(String[] args){
DemoA a = new DemoA();
System.out.println(a.num);
a.sing();
}
}