Day03
键盘录入有三种方式
1)早期的 main(String[] args)
String[]:字符串数组
args:形式参数名
弊端:只能接收字符串
javac Demo.java ---- >Demo.class
java Demo hello world java
2)方式2:JD5以后使用的:Scanner:文本扫描器
导包:import java.util.Scanner;
3)第三种方式:利用的BufferedReader:字符缓冲输入流 (IO流部分)
功能:readLine() :读取一行内容
java.io.BufferedReader;
class Demo{
public static void main(String[] args){
System.out.println(args.length) ;
for(int x = 0 ; x < args.length ; x ++){
System.out.println(args[x]) ;
}
}
}
方法的形式参数传递:
1)基本数据类型作为参数传递:四类八种
形式参数的改变不影响实际参数!
2)引用类型作为参数传递:数组,类,接口
形式参数改变会直接影响实际参数(String除外,它作为参数传递和基本类型作为参数传递效果一致!)
String特点:字符串是常量
class ArgsDemo{
public static void main(String[] args){
//定义两个变量a,b
int a = 10 ;
int b = 20 ;
System.out.println("a:"+a+",b:"+b) ;//10,20
change(a,b) ;
System.out.println("a:"+a+",b:"+b) ;//10,20
System.out.println("---------------------------") ;
//创建一个数组,静态初始化
int[] arr = {1,2,3,4,5} ;
System.out.println(arr[3]) ; //4
change(arr) ;
System.out.println(arr[3]) ; //8
System.out.println("---------------------------") ;
//String作为引用类型(特殊的引用类型)作为参数传递
String s = "hello" ;
System.out.println(s) ;
change(s) ;
System.out.println(s) ;
}
public static void change(String s){
System.out.println(s) ;
s += "gaoyuanyuan" ;
System.out.println(s) ;
}
//重载的change方法
public static void change(int[] arr){
//遍历数组
for(int x = 0 ; x < arr.length ; x ++){
if(arr[x] % 2 ==0){
arr[x] *= 2 ;
}
}
}
public static void change(int a,int b){
System.out.println("a:"+a+",b:"+b) ;//10,20
a = b ; // a = 20
b = a + b ; // b = 20 + 20 = 40
System.out.println("a:"+a+",b:"+b) ;//20,40
}
}
运行结果:
a:10,b:20
a:10,b:20
a:20,b:40
a:10,b:20
---------------------------
4
8
---------------------------
hello
hello
hellogaoyuanyuan
hello
需求:
有一个手机事物,它有 品牌,价格,颜色的属性
手机可以打电话,发短信,玩游戏等行为!
使用面向对象的编程方式:去定义一个手机类,然后在PhoneTest类中进行测试
手机事物:
属性:品牌,价格,颜色
行为:打电话,发短信,玩游戏
定义一个Phone类
成员变量:品牌(brand),价格(price),颜色(phoneColor)
成员方法:callPhone(String name),sendMsg(),playGame()class Phone{ //成员变量 String brand ; //品牌 int price ; //价格 String phoneColor ; //手机颜色 String phoneDesc ;//手机描述信息 //成员方法 public String callPhone(String name){ return "给"+name+"可以打电话" ; } public void sendMsg(){ System.out.println("手机可以发短信...") ; } public void playGame(){ System.out.println("手机可以玩游戏...") ; } } //手机类的测试类 class PhoneTest{ public static void main(String[] args){ //创建Phone类对象 //类名 对象名 = new 类名() ; Phone p = new Phone() ; System.out.println(p) ;//Phone@6d06d69c 地址值 System.out.println(p.brand+"---"+p.price+"---"+p.phoneColor+"---"+p.phoneDesc) ; System.out.println("-------------------------------") ; //给成员变量赋值 p.brand = "华为mate30pro" ; p.price = 6799 ; p.phoneColor = "翡翠绿" ; p.phoneDesc = "为拍照而生,天空之境!" ; System.out.println(p.brand+"---"+p.price+"---"+p.phoneColor+"---"+p.phoneDesc) ; //调用方法 String s = p.callPhone("高圆圆") ; System.out.println(s) ; p.sendMsg() ; p.playGame() ; } }
类和对象:
类:一组事物的属性和行为的集合! 学生类 Student
对应的真实存在事物(泛指) 学生事物
对象: 描述的应该真实事物中的具体的!
学生事物
高圆圆(具体事物),20岁,喜欢lol
代码实现:
创建对象
类名 对象名 = new 类名();
对象名.成员变量= 赋值
对象名.成员方法() ; 使用这个方法System.out.println(p1==p2) ;//引用类型比较:比较的是地址值是否相同!
//int a = 10 ;
//int b = 20 ;
//System.out.println(a == b); //基本数据类型:比较的是数据值是否相同
将p1赋值p3对象
//Phone p3 = p1 ;//没有Phone 找不到符号
局部变量和成员变量的区别?
1)代码的书写位置不同
局部变量:在方法定义中或者是方法声明上的变量
成员变量:类中方法外
2)内存中的位置不同
局部变量:在栈内存中
成员变量:在堆内存中
3)生命周期不同
局部变量:随着方法调用而存在,随着方法的调用结束而消失!(使用完毕,立即消失了)
成员变量:随着对象的创建而存在,随着对象的创建完毕,等待垃圾回收器自动回收而消失!
(对象创建完毕,对象可能被继续使用,所以不会立即消失!)
4)初始化不同:
局部变量:定义的时候,使用前必须初始化; 要么就是直接初始化,不能没有初始化值!
成员变量:随着类的加载而存在,存在系统默认初始化,可以不直接赋值! 也可以赋值(不推荐)
基本类型作为形式参数传递,形式参数的改变不会影响实际参数!
引用类型在作为参数传递,形式参数的改变会直接影响实际参数!(除了String字符串之外)
数组,类,接口
类:
具体类:实际参数传递需要的是该类的对象!(空间地址值)
class Student{
//定义一个成员方法
public void study(){
System.out.println("Good Good Study ,Day Day Up!!!") ;
}
}
//定义一个类StudentDemo类
class StudentDemo{
//有一个成员方法:method
public void method(Student s){//形式参数是Student类型
s.study() ; // 形式参数变量s.study():s应该在传递----学生类的对象!
}
}
//测试类
class StudentTest{
public static void main(String[] args){
//需求:要访问StudentDemo类中method 方法如何访问?
//创建StudentDemo类的对象
StudentDemo sd = new StudentDemo() ;
//method方法中的参数传递:实际参数需要的是Student的具体对象
Student student = new Student();
sd.method(student) ; //实际参数是什么?
}
}
匿名对象:
没有名字的对象!
格式:
new 类名() ;
特点:
1)开发中使用一次即可
2)使用完毕,立即被回收,可以结束内存空间!
这种写法针对:移动端: Android(考虑内存问题)
3)作为参数传递
pc端:推荐:
类名 对象名 = new 类名() ;
class Student{
//成员方法
public void study(){
System.out.println("爱生活,爱Java...") ;
}
}
class StudentDemo{
public void method(Student s){
s.study() ;
}
}
//测试类中
class NoNameDemo{
public static void main(String[] args){
//需求:访问StudentDemo类中method
//分步骤:
//创建StudentDemo类的对象
StudentDemo sd = new StudentDemo() ;
//创建一个具体的学生类对象
Student student = new Student() ;
sd.method(student) ;
System.out.println("-----------------------") ;
//链式编程
//匿名对象(只限于自己玩,开发中正常使用有名字的对象)
new StudentDemo().method(new Student()) ;
}
}