Day08
this 关键字
在当前的公共访问方法中,(setXXX())中给成员变量进行赋值,变量n,a…都不标识符的规则(见名知意!)
Java中规定,在书写一个标准类的时候,为了防止成员变量名称和局部变量名称一致,提供了一个关键字:this
局部变量会隐藏成员变量
this:为了局部变量和成员变量 ,this代表当前该对象的地址值引用!
//定义一个学生类
class Student{
//private修饰
private String name ; //此处系统给定的默认值
private int age ;
//提供对外公共访问方法setXXX()/getXXX()
//public void setName(String n){
//public void setName(String name){ //当局部变量名称和成员变量名称一致的情况,采用就近原则!
// name = name ;
//}
//使用this改进
public void setName(String name){
this.name = name ;
}
public String getName(){
return name ;
}
//public void setAge(int a){
//public void setAge(int age){
// age = age ;
//}
public void setAge(int age){
this.age = age ;
}
public int getAge(){
return age ;
}
//学生学习...
public void study(){
System.out.println("Good Good Study,Day Day Up...");
}
//将成员变量输出
public void show(){
System.out.println(this.name+"----"+this.age) ; //隐藏this
}
}
//测试类
class StudentTest{
public static void main(String[] args){
//创建学生类对象
Student s = new Student() ;
//s.name = "高圆圆" ;
s.setName("马伊琍") ;
s.setAge(30);
//输出
//null---0
System.out.println(s.getName()+"---"+s.getAge()) ;
s.show() ;
}
}
构造方法
构造方法:(属于一个类的成员)
1.没有具体的返回值类型
2.连void都没有
3.方法名和类名相同
作用:就是给当前类进行数据初始化!
构造方法的目的:
为了给当前类的成员进行数据初始化!
构造方法:如果标准类的时候,没有给出构造方法,系统默认会提供无参构造方法
注意:如果提供了有参构造方法,系统不会再提供无参构造方法.(永远给出无参构造方法…)
构造方法是可以重载的!
对于成员变量的赋值有几种方式:
1.setXXX()公共访问方法
2.构造方法进行赋值
类的成员:
1.成员变量
2.构造方法
3.成员方法
标准类的最终版写法
class Student{
//成员变量
private String name ;
private int age ;
//构造方法
//无参构造
public Student(){}
//有参构造
public Student(String name,int age){
this.name = name ;
this.age = age ;
}
//成员方法:
public void setName(String name){
this.name = name ;
}
public String getName(){
return name ;
}
public void setAge(int age){
this.age = age ;
}
public int getAge(){
return age ;
}
//学习
public void study(){
System.out.println("学习Java...") ;
}
}
//测试类
class StudentTest{
public static void main(String[] args){
//方式1:无参构造方法+setXXX()
Student s = new Student() ;
s.setName("高圆圆") ;
s.setAge(27) ;
s.study();
System.out.println(s.getName() +"---"+s.getAge()) ;
System.out.println("---------------------");
//方式2:通过有参构造进行赋值
Student s2 = new Student("赵又廷",29) ;
System.out.println(s2.getName() +"---"+s2.getAge()) ;
s2.study();
}
}
成员方法
有返回值类型的方法
1.带形式参数的
2.无参
没有具体返回值类型的方法:void
1.带形参的
2.无参
面试题:
具体的做了哪些事情?
class Student{
private String name ;
private int age ;
//无返回值,无参
public void show(){
System.out.println("无返回值无参的成员方法...") ;
}
public void function(String s){
System.out.println("world"+s) ;
}
//有返回值类型,无参的
public String method(){
return "helloworld" ;
}
//有具体返回值类型的并且带参数的成员方法
public String method2(int a){
return "javaEE"+a;
}
}
//测试类
class Test{
public static void main(String[] args){
//通过对象调用当前Student类中的成员方法
Student student = new Student() ;
student.show();
student.function("java") ;
String s = student.method();
System.out.println(s) ;
String s2 = student.method2(10) ;
System.out.println(s2) ;
}
}
Static
定义一个 人类:Person
name,age,country(国籍)
定义PersonTest类,进行测试
以下代码,给成员变量赋值,成员变量country都是相同的,就会在内存中消耗内存空间的.
针对这种多个对象共有一个成员变量,Java提供了一个关键字:static
静态(共享,共用)
static修饰成员变量,成员方法
class Person{
String name ;
int age ;
//String country ;
static String country ; //被Person类的多个对象共用!
//无参构造方法(永远给出)
public Person(){}
//带两个name,age的有参构造
public Person(String name,int age){
this.name = name ;
this.age = age ;
}
//三个参数的
public Person(String name ,int age,String country){
this.name = name ;
this.age = age ;
this.country = country ;
}
//show(),显示成员变量
public void show(){
System.out.println("姓名是:"+name+"的人,年龄是:"+age+",国籍是:"+country) ;
}
}
//测试类
class PersonTest{
public static void main(String[] args){
//创建第一个Person类对象
Person p1 = new Person("西施",28,"中国") ;
p1.show() ;
System.out.println("-------------------");
//创建第二个对象
//Person p2 = new Person("貂蝉",20,"中国") ;
Person p2 = new Person("貂蝉",20) ;
p2.show() ;
System.out.println("-------------------");
//Person p3 = new Person("王昭君",29,"中国") ;
Person p3 = new Person("王昭君",29) ;
p3.show() ;
System.out.println("-------------------");
p3.country = "美国" ;
p3.show() ;
p1.show() ;
p2.show() ;
}
}
面试题
static的特点:
1.随着类的加载而加载
想一想:main() ---->public static void main(String[] args){}
2.static修饰的变量/方法,优先与对象存在!
回想:对比Student s = new Student(); 完成了哪些事情
3.被static修饰的具有共享,共用(多个对象共同访问同一个static修饰的成员变量)
什么时候去使用static关键字…
举例:类似的这些,都是用static修饰
班级编号(共享)
饮水机 (共享)
水杯(不能被共享的)
4.被static修饰的变量或者成员方法,可以直接被类名调用 (使用API,很多成员方法都是static修饰)
Arrays:数组工具类
Arrays.binarySearch(int[] arr) ;
Arrays.sort(int[] arr) ;
main(…){
Student s = new Student() ;
加载Student.class ----->在当前类中有关被static修饰的都会进入内存
}
static
this
class Demo{
//成员变量
int num = 100 ;
static int num2 = 50 ;
//show()方法
public void show(){ //非静态的方法
System.out.println(num) ; //隐含的访问成员变量 100
System.out.println(this.num) ;//显示的访问成员变量 100
System.out.println(num2) ;//50
}
public void function(){
System.out.println("function...") ;
}
//静态的成员方法
public static void method(){
//无法从静态上下文中引用非静态 变量 num
//编译不通过:非静态变量
//System.out.println(num) ;
//编译不通过: 无法从静态上下文中引用非静态 方法 function()
//function() ;
System.out.println("method...") ;
}
}
class StaticDemo{
public static void main(String[] args){ //jvm识别
//创建对象
Demo d = new Demo();
d.show();
//被static的变量,可以类名.成员变量;
System.out.println(Demo.num2) ;
d.method() ;
//被static修饰的成员方法,可以被类名.方法名()
Demo.method() ;
}
}
static关键字的注意事项:
1.在静态的方法中是没有this关键字的!
静态优先于对象存在
2.一句话:
静态只能访问静态的变量/方法
非静态的成员方法:
可以访问非静态成员变量,静态的成员变量
可以访问静态的成员方法,非静态的成员方法
静态的成员方法:
只能访问静态成员变量,只能访问静态成员方法
class Demo2{
public int num = 10 ;
public static int num2 = 20 ; //和类有关系
public void method(){
System.out.println(num) ;//10 this.num
System.out.println(num2) ;//20
}
public static void function(){
//System.out.println(this.num) ; //静态方法中不能存在this
//System.out.println(num) ;//无法从静态上下文中引用非静态 变量 num
System.out.println(num2);
}
//非静态的成员方法
public void function2(){
function() ;
method() ;
}
public static void show(){
//method() ;
function();
}
}
class StaticDemo2{
public static void main(String[] args){
Demo2 d = new Demo2() ;
d.method() ;
d.function() ;
System.out.println("----------");
d.function2() ;
System.out.println("----------");
d.show() ;
System.out.println("----------");
int result = sum(100,50) ;
System.out.println(result) ;
}
//求两个数据之和
public static int sum(int a,int b){
return a+b ;
}
}
main解释
public static void main(String[] args): jvm识别的程序的入口
public :权限修饰符,公共的,公开的,保证访问权限足够大
static:静态修饰,和类有关系,优先对象存在
void:由jvm调用,没有具体的返回值类型 ,使用void代替
main:方法名, 编程语言,大部分都是有一个程序的入口,Java叫main(主方法)
String[] args: 接收字符串数组,参数名args,
Jdk 1.0版本
早期接收键盘录入数据的.
java 类名 数据1 数据2 数据3....
args[0]
args[1]
args[2]
Jdk:1.5以后,可以使用Scanner(System.in) ;
高级录入:
BufferedReader:字符输入流
class MainDemo{
public static void main(String[] args){
//输出args
//System.out.println(args) ; //[Ljava.lang.String;@6d06d69c
//System.out.println(args[0]) ; //ava.lang.ArrayIndexOutOfBoundsException :数组角标越界异常
//int[] arr = {元素1,元素2,元素3....} ;
args = new String[3];
args[0] = "hello" ;
args[1] = "world" ;
args[2] = "java" ;
//遍历数组
for(int x = 0 ; x < args.length; x ++){
System.out.println(args[x]) ;
}
}
}
文档说明书制作
测试类:
ArrayDemo
测试遍历的数组
class ArrayDemo{
public static void main(String[] args){
//定义一个数组,静态初始化
int[] arr = {11,22,44,55,66} ;
//遍历的数组
/*
for(int x = 0 ; x < arr.length; x ++){
System.out.print(arr[x]+" ") ;
}
*/
//单独调用
//printArray(arr) ;
//去掉static,如何访问
//只能通过对象来访问
//ArrayDemo ad = new ArrayDemo() ;
//ad.printArray(arr) ;
//上述代码虽然可以,但是当前测试类的目的,创建其他对象,使用其他对象
//改进:可以将遍历的代码块定义到别的类中
//创建ArrayTool类的对象
//ArrayTool at = new ArrayTool() ;
//at.printArray(arr) ;
//上面这个是通过无参构造方法创建的对象,如何将对象的创建(无参构造方法)禁止调用? private关键字
//编译不通过,被private修饰了
//类名.成员方法() ;
ArrayTool.printArray(arr) ;
}
//将遍历的操作,使用功能代码块包起来
//public static void printArray(int[] arr){
/*
public void printArray(int[] arr){
System.out.print("[") ;
for(int x = 0 ; x < arr.length; x ++){
if(x==arr.length-1){
System.out.println(arr[x]+"]") ;
}else{
System.out.print(arr[x]+", ");
}
}
}
*/
}
class ArrayTool{
//无参构造
//public ArrayTool(){}
private ArrayTool(){}
//如果构造方法私有了,只能给成员方法加入static修饰
public static void printArray(int[] arr){
System.out.print("[") ;
for(int x = 0 ; x < arr.length; x ++){
if(x==arr.length-1){
System.out.println(arr[x]+"]") ;
}else{
System.out.print(arr[x]+", ");
}
}
}
}