THIS关键字:
this:看上去是用于区分局部变量和成员变量同名情况。
this代表它所在函数所属对象的引用。简单说那个对象在调用this所在的函数,this就代表那个对象。
this用于构造函数间的互相调用,this的调用必须是构造函数中的第一个语句。因为初始化要先执行。
this关键字的应用:比较年龄是不是同龄人。
public static void main(String[] args) {
//Person p=new Person("张三");
//Person p1=new Person("李四");
//p1.speak();
Person p1=new Person(20);
Person p2=new Person(25);
boolean b = p1.compare(p2);
System.out.println(b);
}
}
class Person{
private String name;
private int age;
/*构造代码块
作用:给对象进行初始化。
对象一建立就运行,而且优先于构造函数执行。
和构造函数的区别:
构造代码块是给所有对象进行统一初始化,而构造函数是给对应的对象初始化
{
System.out.println("Crying....");
}
*/
public Person(){
//System.out.println("name="+name+",age="+age);
}
public Person(int age) {
this.age = age;
}
public Person(String name) {
this.name=name;
//System.out.println("name="+name+",age="+age);
}
public void speak() {
System.out.println("name="+name+",age="+age);
}
public boolean compare(Person p) {
return this.age ==p.age;
}
}
静态static
用法:是一个修饰符,用于修饰成员(成员变量和成员函数)
当成员被修饰后,就多了一个调用方式,除了可以被对象调用外,还可以被类名直接调用,类名.静态成员
还可以直接被类名调用,类名.静态成员
特点:1.随着类的加载而加载(也就是说会随着类的的消失而消失,说明生命周期长)。2.优先于对象存在(静态是先存在的,对象是后存在的)。3.被所有对象所共享。4.可以直接被类名所调用。
public static void main(String[] args) {
System.out.println(Person.country);//可以直接被类名所调用
}
}
class Person{
String name;//成员变量,实例变量
static String country="CN";//静态的成员变量,类变量。
public void show (){
System.out.println(name+","+country);
}
}
实例变量和类变量的区别:
1.存放位置。类变量随着类的加载而存在于方法区中。实例变量随着对象的建立而存在于堆内存中。
2.生命周期。类变量生命周期最长,随着类的消失而消失。实例变量生命周期随着对象的消失而消失。
静态使用注意事项:
1.静态方法只能访问静态成员(非静态方法既可以访问静态也可以访问非静态)。
2.静态方法中不可以定义this关键字,因为静态优先于对象存在,所以静态方法中不可以出现this。
3.主函数是静态的。
静态方法有利有弊:
利:对对象的共享数据进行单独空间的存储节省空间。没有必要每个空间都存储一份。
可以直接被类名调用。
弊:1.生命周期长。2.访问出现局限性
主函数:
public static void main(String[] args) {
/*public static void main(String[] args)
主函数:是一个特殊的函数,作为程序的入口,可以被JVM调用。
主函数的定义:
public:代表着该函数访问权限是最大的
static:代表主函数随着类的加载就已经存在了
void:主函数没有具体的返回值。
main:不是关键字,但是是一个特殊的单词,可以被JVM识别
(String[] args):函数的参数,参数类型是一个数组,该数组中的元素是字符串,字符串类型的数组。
主函数是固定格式的,JVM识别。
jvm在调用主函数时,传入的是new string[0]
*/
}
jvm在调用主函数时,传入的是new string[0]:
public class MainDamo {
public static void main(String[] args) {
//public static void main(String[] args)
//主函数:是一个特殊的函数,作为程序的入口,可以被JVM调用。
String [] arr= {"hha","heihei","hhe"};
MainTest.main(arr);
}
}
class MainTest{
public static void main(String[] args) {
for(int x=0;x<args.length;x++) {
System.out.println(args[x]);
}
}
}
什么时候定义静态函数呢?
静态可以直接访问静态属性或者方法,如果静态方法想访问非静态的属性或者方法,只能先new出对象让对象去调用或者把属性或方法改成静态的
public static void main(String[] args) {
Person.show();
// Person p = new Person();
// p.name="张三";
// p.show();
}
}
class Person{
static String name;//静态属性
public static void show() //静态方法
{
System.out.println(name+"hha");
}
}
静态的应用:
public static void main(String[] args) {
int [] a = {1,2,3};
int [] b = {4,5,6};
int max=getMax(a);
int max1=getMax(b);
System.out.println("max="+max+",max1="+max1);
}
public static int getMax(int [] a) {
int max = 0;
for(int x = 1;x < a.length; x++) {
if(a[x]>max) {
max=x;
}
}
return a[max];
}
}
public static void main(String[] args) {
int [] a = {3,2,1,4,6};
int max = Study.getMax(a);
System.out.println("max="+max);
int min = Study.getMin(a);
System.out.println("min="+min);
//Study.bubbleSort(a);
Study.printArray(a);
System.out.println();
Study.bubbleSort(a);
Study.printArray(a);
}
}
class Study{//获取数组中的最大值
public static int getMax(int [] a) {
int max = 0;
for(int x = 1;x <a.length;x++) {
if(a[x]>a[max])
max=a[x];
}
return a[max];
}
//获取数组中的最小值
public static int getMin(int [] a) {
int min = 0;
for(int x = 1;x <a.length;x++) {
if(a[x] < a[min])
min=a[x];
}
return a[min];
}
public static void selectSort(int [] a) {
for(int x = 0; x < a.length-1;x++) {
for(int y = x+1;y < a.length;y++) {
if(a[x] > a[y]) {
swap(a,x,y);
}
}
}
}
public static void bubbleSort(int [] a) {
for(int x = 0; x < a.length-1;x++) {
for(int y = 0;y < a.length-x-1;y++) {
if(a[y] > a[y+1]) {
swap(a,y,y+1);
}
}
}
}
/**
给数组中元素进行位置的置换
*/
public static void swap(int [] a,int x,int y) {
int temp = a[x];
a[x]=a[y];
a[y]=temp;
}
/**
用于打印数组中的元素,打印格式:[emt1,element2]
*/
public static void printArray(int [] a) {
System.out.print("[");
for(int x = 0;x < a.length;x++) {
if(x!=a.length-1) {
System.out.print(a[x]+",");
}
else {
System.out.print(a[x]+"]");
}
}
}
}
结果:
静态代码块:
格式:static{ 静态代码块中的执行语句
}
特点:随着类的加载而执行,只执行一次。用于给类进行初始化,并优先于主函数。
class StaticCode{
static
{
System.out.println("a");
}
}
public class StaticCodeDemo {
static
{
System.out.println("b");
}
public static void main(String[] args) {
new StaticCode();
new StaticCode();
System.out.println("over");
}
static{
System.out.println("c");
}
}
打印的结果是b c a over
对象的初始化过程: