怎么建立公司网站费用,江苏企业网站排名优化,贵阳花溪区,网站做的不满意文章目录 1. 类和对象的基本概念1.1 JAVA是面向对象语言1.2 类和对象的描述 2. 类与对象的定义与使用2.1 类的定义格式2.2 类的实例化(对象的创建)2.3 举个例子 3. 对象的构造及初始化3.1构造方法3.1.1构造方法的定义3.1.2 构造方法的特性 4.2 默认初始化5.4 就地初始化 4.this… 文章目录 1. 类和对象的基本概念1.1 JAVA是面向对象语言1.2 类和对象的描述 2. 类与对象的定义与使用2.1 类的定义格式2.2 类的实例化(对象的创建)2.3 举个例子 3. 对象的构造及初始化3.1构造方法3.1.1构造方法的定义3.1.2 构造方法的特性 4.2 默认初始化5.4 就地初始化 4.this 关键字表示当前对象的引用4.1 this.XXX调用当前对象的属性/字段/成员变量4.2 this.xxx()调用当前对象的成员方法4.3 this(XX,...)调用当前对象的其他构造方法4.4 this引用 5.对象的打印6. 封装6.1 封装的概念6.2 访问限定符 7. static关键字7.1 static修饰成员变量7.2 static修饰成员方法7.4 static成员变量初始化 1. 类和对象的基本概念
1.1 JAVA是面向对象语言
Java是一门纯面向对象的语言(Object Oriented Program简称OOP)在面向对象的世界里一切皆为对象。面向对象是解决问题的一种思想主要依靠对象之间的交互完成一件事情。
1.2 类和对象的描述
在Java中万物皆对象一切都围绕对象进行 类把具有相同属性和行为的一类对象抽象为类。类是抽象概念如人类、犬类等无法具体到每个实体。 对象某个类的一个实体当有了对象后这些属性便有了属性值行为也就有了相应的意义。 类是对象的抽象而对象是类的具体实例。类是抽象的不占用内存而对象是具体的占用存储空间。 做个比方类实例化出对象就像现实中使用建筑设计图建造出房子类就像是设计图只设计出需要什么东西但是并没有实体的建筑存在同样类也只是一个设计实例化出的对象才能实际存储数据占用物理空间。
2. 类与对象的定义与使用
面向对象程序设计关注的是对象而对象是现实生活中的实体比如狗。但是狗计算机并不认识需要开发人员告诉给计算机什么是狗。 上图右侧就是对狗简单的描述该过程称为对狗对象(实体)进行抽象(对一个复杂事物的重新认知)但是这些简化的抽象结果计算机也不能识别开发人员可以采用某种面向对象的编程语言来进行描述比如Java语言。
2.1 类的定义格式
在java中定义类时需要用到class关键字class为定义类的关键字ClassName为类的名字{}中为类的主体。具体语法如下
// 创建类
class ClassName{ field; // 字段(属性) 或者 成员变量method; // 行为 或者 成员方法
}类中包含的内容称为类的成员。属性主要是用来描述类的称之为类的成员属性或者类成员变量。方法主要说明类具有哪些功能称为类的成员方法。
2.2 类的实例化(对象的创建)
定义了一个类就相当于在计算机中定义了一种新的类型有了这些自定义的类型之后就可以使用这些类来定义实例(或者称为对象)。用类类型创建对象的过程称为类的实例化在java中采用new关键字配合类名来实例化对象。
类名称 对象名称 new 类名称();PetDog dogs new PetDog();注意事项 new 关键字用于创建一个对象的实例. 2.3 举个例子
采用Java语言定义狗类型并创建一个狗的对象
class PetDog {//成员变量实例变量)在堆中的每个对象中存储通过对象调用// 狗的属性public String name;//名字public String color;//颜色//成员方法实例方法在JVM的方法区中存储通过对象调用// 狗的行为public void barks() {System.out.println(name : 旺旺旺~~~);}public void wag() {System.out.println(name : 摇尾巴~~~);}
} public class Main{public static void main(String[] args) {//创建一个实例化对象通过new实例化对象PetDog dogh new PetDog(); //通过对象来调用实例变量、成员方法 dogh.name 阿黄;dogh.color 黑黄;dogh.barks();dogh.wag();PetDog dogs new PetDog();dogs.name 阿黄;dogs.color 黑黄;dogs.barks();dogs.wag();}
}输出结果
阿黄: 旺旺旺~~~
阿黄: 摇尾巴~~~
赛虎: 旺旺旺~~~
赛虎: 摇尾巴~~~注意事项: 类名注意采用大驼峰定义一般一个文件当中只定义一个类 main方法所在的类一般要使用public修饰(注意Eclipse默认会在public修饰的类中找main方法)public修饰的类必须要和文件名相同 ,并且不要轻易去修改public修饰的类的名称。使用 . 来访问对象中的属性和方法.同一个类可以创建多个实例. 3. 对象的构造及初始化
3.1构造方法
3.1.1构造方法的定义
构造方法(也称为构造器)是一个特殊的成员方法名字必须与类名相同在创建对象时由编译器自动调用并且在整个对象的生命周期内只调用一次。 构造方法规则 名字与类名相同没有返回值类型设置为void也不行 一般情况下使用public修饰 一个类中至少存在一个构造方法若没有显示定义编译器会生成一个默认的无参构造 示例代码
public class Date {public int year;public int month;public int day;public Date(int year, int month, int day){this.year year;this.month month;this.day day;System.out.println(Date(int,int,int)方法被调用了);}public void printDate(){System.out.println(year - month - day);}public static void main(String[] args) {// 此处创建了一个Date类型的对象并没有显式调用构造方法Date d new Date(2021,6,9); // 输出Date(int,int,int)方法被调用了d.printDate(); // 2021-6-9}
}使用new关键字产生一个对象时大致分为以下两步4.2中有详细过程 1为对象在堆中分配空间空间大小由该类中成员变量的属性决定 2调用对象的构造方法为对象成员变量赋值(当构造方法调用结束后该对象初始化完成)构造方法的作用就是对对象中的成员进行初始化并不负责给对象开辟空间。
3.1.2 构造方法的特性
名字必须与类名相同没有返回值类型设置为void也不行创建对象时由编译器自动调用并且在对象的生命周期内只调用一次(相当于人的出生每个人只能出生一次)构造方法可以重载(用户根据自己的需求提供不同参数的构造方法)
class Date {public int year;public int month;public int day;// 无参构造方法public Date(){this.year 1900;this.month 1;this.day 1;}// 带有三个参数的构造方法public Date(int year, int month, int day) {this.year year;this.month month;this.day day;}
上述两个构造方法名字相同参数列表不同因此构成了方法重载。
如果用户没有显式定义编译器会生成一份默认的构造方法生成的默认构造方法一定是无参的。
class Date {public int year;public int month;public int day;public void printDate(){System.out.println(year - month - day);}public static void main(String[] args) {Date d new Date();d.printDate();}
} 上述Date类中没有定义任何构造方法编译器会默认生成一个不带参数的构造方法。
如果用户显式定义了构造方法编译器则不会再生成构造方法。
class Date {public int year;public int month;public int day;public Date(int year, int month, int day) {this.year year;this.month month;this.day day;}
}public class Test{public static void main(String[] args) {// 如果编译器会生成则生成的构造方法一定是无参的则此处创建对象是可以通过编译的// 但实际情况是编译期报错Date d new Date();}
} 4.2 默认初始化
public class Date {public int year;public int month;public int day;public Date() {// 成员变量在定义时并没有给初始值, 为什么就可以使用呢System.out.println(this.year);System.out.println(this.month);System.out.println(this.day);}public static void main(String[] args) {// 此处a没有初始化编译时报错// Error:(24, 28) java: 可能尚未初始化变量a// int a;// System.out.println(a);Date d new Date();}
}我们观察上述代码部分发现局部变量在使用时必须要初始化而成员变量可以不用。要搞清楚这个问题的答案就需要知道 new 关键字背后所发生的一些事情
Date d new Date();在程序层面只是简单的一条语句在JVM层面需要做好多事情下面简单介绍下 检测对象对应的类是否加载了如果没有加载则加载 为对象分配内存空间 处理并发安全问题 比如多个线程同时申请对象JVM要保证给对象分配的空间不冲突 初始化所分配的空间 即对象空间被申请好之后对象中包含的成员已经设置好了初始值比如
数据类型默认值byte0char‘\u0000’short0int0long0Lbooleanfalsefloat0.0fdouble0.0reference(引用类型)null
设置对象头信息调用构造方法给对象中各个成员赋值
5.4 就地初始化
在声明成员变量时就直接给出了初始值。
public class Date {public int year 1900;public int month 1;public int day 1;public Date(){}public Date(int year, int month, int day) {}public static void main(String[] args) {Date d1 new Date(2021,6,9);Date d2 new Date();}
}注意代码编译完成后编译器会将所有给成员初始化的这些语句添加到各个构造函数中
4.this 关键字表示当前对象的引用
4.1 this.XXX调用当前对象的属性/字段/成员变量
class Date {public int year;public int month;public int day;public Date(int year, int month, int day) {year year;month month;day day;}public void printDate() {System.out.println(year / month / day);}
}
public class Test{public static void main(String[] args) {// 构造日期类型的对象 d1Date d1 new Date(2020, 9, 15);// 打印日期中的内容d1.printDate();}
} 以上代码定义了一个日期类然后main方法中创建了三个对象并通过Date类中的构造方法对对象成员变量进行初始化但输出结果仍是默认值。 原因是这里传入的形参名 year和 monthday与 赋值的名字相同 编译器并不知情 会按照就近匹配原则由于局部变量优先 ,此时就会出现自己赋值给自己的情况 。 this关键字表示当前对象的引用使用this关键字可以调用当前对象的成员变量
class Date {public int year;public int month;public int day;public Date(int year, int month, int day){this.year year;this.month month;this.day day;}public void printDate(){System.out.println(this.year / this.month / this.day);}
}public class Test{public static void main(String[] args) {Date d1 new Date(2020, 9, 15);// 打印日期中的内容d1.printDate();}
}4.2 this.xxx()调用当前对象的成员方法
class Date {public int year;public int month;public int day;public Date(int year, int month, int day){this.year year;this.month month;this.day day;}public void init() {System.out.println(Date的int()方法);}public void printDate(){//init()方法是成员方法必须通过对象调用//this表示对象的引用调用成员方法时不写编译时也会自动加上init();//等价于this.init();System.out.println(Date的printDate()方法);System.out.println(this.year / this.month / this.day);}
}public class Test{public static void main(String[] args) {Date d1 new Date(2020, 9, 15);// 打印日期中的内容d1.printDate();}
}4.3 this(XX,…)调用当前对象的其他构造方法
构造方法中可以通过this调用其他构造方法来简化代码
class Date {public int year;public int month;public int day;// 无参构造方法--内部给各个成员赋值初始值该部分功能与三个参数的构造方法重复// 此处可以在无参构造方法中通过this调用带有三个参数的构造方法// 但是this(1900,1,1);必须是构造方法中第一条语句public Date(){//System.out.println(year); 注释取消掉编译会失败,this()是构造方法中第一条语句this(1900, 1, 1); //等价于//this.year 1900;//this.month 1;//this.day 1;}// 带有三个参数的构造方法public Date(int year, int month, int day) {this.year year;this.month month;this.day day;}
}注意事项 this(…)必须是构造方法中第一条语句不能形成环 public Date(){this(1900,1,1);
}
public Date(int year, int month, int day) {this();
}/*无参构造器调用三个参数的构造器而三个参数构造器有调用无参的构造器形成构造器的递归调用
编译报错Error:(19, 12) java: 递归构造器调用*/
4.4 this引用
this引用指向当前对象(成员方法运行时调用该成员方法的对象)在成员方法中所有成员变量的操作都是通过该引用去访问。只不过所有的操作对用户是透明的即用户不需要来传递编译器自动完成。
class Date {public int year;public int month;public int day;public void setDay(int year, int month, int day){this.year year;this.month month;this.day day;}public void printDate(){System.out.println(this.year / this.month / this.day);}
}
public class Test{public static void main(String[] args) {Date d new Date();d.setDay(2020,9,15);d.printDate();}
}注意this引用的是调用成员方法的对象。
this的类型对应类类型引用即哪个对象调用就是哪个对象的引用类型this只能在成员方法中使用在成员方法中this只能引用当前对象不能再引用其他对象this是“成员方法”第一个隐藏的参数编译器会自动传递在成员方法执行时编译器会负责将调用成员方法对象的引用传递给该成员方法this负责来接收
5.对象的打印
class Person {String name;String gender;int age;public Person(String name, String gender, int age) {this.name name;this.gender gender;this.age age;}}public class Test{public static void main(String[] args) {Person person new Person(Jim,男, 18);System.out.println(person);}
}// 打印结果day20210829.Person1b6d3586如果想要默认打印对象中的属性该如何处理呢答案重写toString方法即可。
class Person {String name;String gender;int age;public Person(String name, String gender, int age) {this.name name;this.gender gender;this.age age;}Overridepublic String toString() {return [ name , gender , age ];}
} public class Test{public static void main(String[] args) {Person person new Person(Jim,男, 18);System.out.println(person);}
}// 输出结果[Jim,男,18]6. 封装
6.1 封装的概念
面向对象程序三大特性封装、继承、多态。而类和对象阶段主要研究的就是封装特性. 封装将数据和操作数据的方法进行有机结合隐藏对象的属性和实现细节仅对外公开接口来和对象进行交互 6.2 访问限定符
Java中主要通过类和访问权限来实现封装类可以将数据以及封装数据的方法结合在一起更符合人类对事物的认知而访问权限用来控制方法或者字段能否直接在类外使用。Java中提供了四种访问限定符 【说明】
protected主要是用在继承中default权限指什么都不写时的默认权限访问权限除了可以限定类中成员的可见性也可以控制类的可见性
public class Computer {private String cpu; // cpuprivate String memory; // 内存public String screen; // 屏幕String brand; // 品牌----default属性public Computer(String brand, String cpu, String memory, String screen) {this.brand brand;this.cpu cpu;this.memory memory;this.screen screen;}public void Boot(){System.out.println(开机~~~);}public void PowerOff(){System.out.println(关机~~~);}public void SurfInternet(){System.out.println(上网~~~);}
}public class TestComputer {public static void main(String[] args) {Computer p new Computer(HW, i7, 8G, 13*14);System.out.println(p.brand); // default属性只能被本包中类访问System.out.println(p.screen); // public属性 可以任何其他类访问// System.out.println(p.cpu); //err private属性只能在Computer类中访问不能被其他类访问}
}注意一般情况下成员变量设置为private成员方法设置为public。
7. static关键字
7.1 static修饰成员变量
static修饰的成员变量称为静态成员变量静态成员变量最大的特性不属于某个具体的对象是所有对象所共享的。 【静态成员变量特性】 1. 不属于某个具体的对象是类的属性所有对象共享的不存储在某个对象的空间中 2. 既可以通过对象访问也可以通过类名访问但一般更推荐使用类名访问 3. 类变量存储在方法区当中 4. 生命周期伴随类的一生(即随类的加载而创建随类的卸载而销毁) public class Student{public String name;public String gender;public int age;public double score;public static String classRoom Bit306;// ...public static void main(String[] args) {// 静态成员变量可以直接通过类名访问System.out.println(Student.classRoom);Student s1 new Student(Li leilei, 男, 18, 3.8);Student s2 new Student(Han MeiMei, 女, 19, 4.0);Student s3 new Student(Jim, 男, 18, 2.6);// 也可以通过对象访问但是classRoom是三个对象共享的System.out.println(s1.classRoom);System.out.println(s2.classRoom);System.out.println(s3.classRoom);}
}调试方式运行上述代码然后在监视窗口中可以看到静态成员变量并没有存储到某个具体的对象中。
7.2 static修饰成员方法
一般类中的数据成员都设置为private而成员方法设置为public那设置之后Student类中classRoom属性如何在类外访问呢
public class Student{private String name;private String gender;private int age;private double score;private static String classRoom Bit306;// ...
}public class TestStudent {public static void main(String[] args) {System.out.println(Student.classRoom);}
}编译失败
Error:(10, 35) java: classRoom 在 extend01.Student 中是 private 访问控制Java中被static修饰的成员方法称为静态成员方法是类的方法不是某个对象所特有的。静态成员一般是通过静态方法来访问的。
public class Student{// ...private static String classRoom Bit306;// ...public static String getClassRoom(){return classRoom;}
}public class TestStudent {public static void main(String[] args) {System.out.println(Student.getClassRoom());}
}输出Bit306【静态方法特性】 1. 不属于某个具体的对象是类方法 2. 可以通过对象调用也可以通过类名.静态方法名(…)方式调用更推荐使用后者 3. 不能在静态方法中访问任何非静态成员变量 public static String getClassRoom(){System.out.println(this);return classRoom;
}// 编译失败Error:(35, 28) java: 无法从静态上下文中引用非静态 变量 thispublic static String getClassRoom(){age 1;return classRoom;
}// 编译失败Error:(35, 9) java: 无法从静态上下文中引用非静态 变量 age 4. 静态方法中不能调用任何非静态方法因为非静态方法有this参数在静态方法中调用时候无法传递this引用 public static String getClassRoom(){doClass();return classRoom;
}// 编译报错Error:(35, 9) java: 无法从静态上下文中引用非静态 方法 doClass()5. 静态方法无法重写不能用来实现多态 7.4 static成员变量初始化
注意静态成员变量一般不会放在构造方法中来初始化构造方法中初始化的是与对象相关的实例属性,静态成员变量的初始化分为两种就地初始化 和 静态代码块初始化。
就地初始化 就地初始化指的是在定义时直接给出初始值
public class Student{ private static String classRoom Bit306; // ...
}静态代码块初始化
public class Student{ private static String classRoom; // 静态代码块static {classRoom bit306;System.out.println(I am static init()!);}