2016-1-4日志

何李娇 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(People p){
   			    声明对象
		p = new People();
		    构建对象
		p.age =35;
	}遇到这个大括号的时候声明的p对象作用域结束
	
	public static void main(String[] args){
		People p = new People();
                声明对象     构建对象
		//p.age = 15
		System.out.println(p.age);      
		p.change(p);
		//p.age = 25
		System.out.println(p.age);
		
		p.change2(p);
		//p.age = 25
		System.out.println(p.age);
Stack   Heap        
 
 
p    new People() age =25
p    new People() age =35
change2(People p)
    p    new People() age=15
    p                =25
change(People p)
	}
	
}
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 okstr 
 arra b c g
 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(); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值