1.java是面向对象的编程
洗衣服分开看就是四个对象:人,衣服,洗衣机,洗衣粉
整个洗衣服的过程就是:人把衣服放进洗衣机,倒入洗衣粉,启动洗衣机,洗衣机就会完成洗衣服的过程并且甩干。
而整个过程主要是:人,衣服,洗衣机,洗衣粉四个对象之间交互完成的,人不需要关心洗衣机具体是怎么洗衣服的,如何甩干的。
以面向对象的方式来处理,就不关心洗衣服的过程,是通过对象之间交互来完成的。
2.类定义和使用
类的定义格式
注意:
1.一般一个文件中只定义一个类
2.main方法所在的类一般要使用public来修饰
3.public修饰的类必须要和文件名相同
4.不要轻易去修改public修饰的类的名称,如果要修改,通过开发工具去修改
3.类的实例化
class Dog{ //成员变量 public String name; public String color; public void barks(){ int count = 0;//局部变量 System.out.println(name + "在狗叫"); } public void wag(){ System.out.println(name + "在摇尾巴"); } } public class OOp { public static void main(String[] args) { //实例化一个Dog对象 Dog dog = new Dog(); //int a = 10; dog.name = "来福"; dog.color = "blue"; System.out.println(dog.name); System.out.println(dog.color); dog.wag(); dog.barks(); Dog dog1 = new Dog(); System.out.println("dog.name"); //当成员变量没有初始化的时候,默认值就是对应的初始值。 //引用类型-》null,int-》0,float-》0.0f,boolean-》false,char-》'\u0000' } }
通过一个类可以实例化无数个对象
4.类和对象的说明
(1)类只是一个模型一样的东西,用来对一个实体进行描述,限定了类有哪些成员。
(2)类是一种自定义的类型,可以用来定义变量。
(3)一个类可以实例化处多个对象,实例化出的对象,占用实际的物理空间,存储成员变量。
(4)做个比方,类实例化出的对象就像现实中使用建筑设计图建造出房子,类就像是设计图,只设计处需要什么东西,但是并没有实体建筑存在,同样类也只是一个设计,实例化出的对象才能实际存储数据,占用物理空间
5.this的使用
public class TestDate { public int year; public int month; public int day; public void setDate(int y,int m,int d){ year = y; month = m; day = d; } public void printDate(){ System.out.println(year + " - " + month + " - " + day); } public static void main(String[] args) { TestDate testDate = new TestDate(); testDate.setDate(2022,12,24); testDate.printDate(); } }
----------------------------------------------------------------------------------------------------------------
假设要改为
public void setDate(int year,int month,int day){ year = year; month = month; day = day; }
得出的结果就不是2022-12-24,而是0-0-0;这是因为在setDate当中,局部变量是优先的,他只是自己给形参自己赋值了,根本没有赋值到成员变量中,此时要加入this来让他认识自己
public void setDate(int year,int month,int day){ this.year = year; this.month = month; this.day = day; }
------------------------------------------------------------------------------------------------------------------------------
this调用的是成员方法的对象
this引用的特性:
this的类型:对应类类型引用,即哪个对象调用就是哪个对象的引用类型
this只能在“成员方法”中使用
在“成员方法”中,this只能引用当前对象,不能再引用其他对象
this是“成员方法”第一个隐藏的参数,编译器会自动传递,在成员犯法执行时,编译器会负责调用成员方法。对象的引用传递给成员方法,this负责来接收
还有一种方法是this(),它用于构造方法的第一行,调用本类当中,其他的构造方法。
6.对象的构造及其初始化
默认初始化
//当成员变量没有初始化的时候,默认值就是对应的初始值。 //引用类型-》null,int-》0,float-》0.0f,boolean-》false,char-》'\u0000'
就地初始化
public String name = "bit";
public age = "99";
构造方法
public class Student { public int age; public String name; public void setStudent(String name,int age){ this.age = age; this.name = name; } public Student(){ System.out.println("不带参数的构造方法"); } public Student(String name ,int age){ System.out.println("带两个参数的构造方法"); this.name = name; this.age = age; } public void print(){ System.out.println(this.name + " * " + this.age); } public static void main(String[] args) { Student student = new Student(); Student student1 = new Student("fjq",1); student.setStudent("fengjunqing",1); student.print(); } }
结果:
不带参数的构造方法
带两个参数的构造方法
fengjunqing * 1
他这个构造方法实在什么时候被使用的呢?
当构造方法调用完成之后,方法才产生了
构造方法可以被重载
构造方法概念
构造方法也称为构造器,是一个特殊的成员方法,名字必须与类名相同,没有返回值类型,在创建对象时,由于编译器自动调用,并且在整个对象的生命周期中只调用一次。
一旦用户定义,编译器将不在自动生成
public Student(){ this("王五",99); //调用本类当中,其他的构造方法 System.out.println("不带参数的构造方法"); } public Student(String name ,int age){ //不能在用this(),不能循环调用 System.out.println("带两个参数的构造方法"); this.name = name; this.age = age; }
使用this(),必须放在构造方法中,而且必须是第一行,并且不能形成环。
快捷写构造方法
public TestDate(){} public TestDate(int year, int month, int day) { this.year = year; this.month = month; this.day = day; }
鼠标右键
Generete,Constructor
然后点想创建啥构造方法
7.封装
访问限定符
public:可以理解为一个人的外貌特征,谁都能看得到
dafault:对于家族中(同一个包中)不是什么秘密,对于其他人来说就是隐私了
private:只有自己知道,其他人都不知道
访问修饰限定符,只是在权限上进行了限制
并没有说不能用在成员变量,成员方法,构造方法上
概念
面向对象程序的三大特性:继承,封装和多态
封装:将数据和操作数据的方法进行有机结合,隐藏对象的属性和实现细节,仅对外公开接口来和对象进行交互(对类内部的实现细节进行了隐藏和封装,对外提供公开接口,供其他用户访问)
private的使用:
class Person1{ public String name; public int age; public void show(){ System.out.println("姓名 " + name + "年龄 " + age); } } public class Test2 { public static void main(String[] args) { Person1 person1 = new Person1(); person1.name = "bit"; } }
我在使用这个person1的时候,人家不叫public String name了,叫public String name1了,紧接着我也得改。下面来实现封装。
class Person1{ private String name; private int age; public void setName(String name){ this.name = name; } public String getName(){ return this.name; } public void show(){ System.out.println("姓名 " + name + "年龄 " + age); } } public class Test2 { public static void main(String[] args) { Person1 person1 = new Person1(); person1.setName("fjq"); System.out.println(person1.getName()); } }
懒得写,让编译器自己生成,鼠标右键Generate,Getter and Setter然后选需要生成的对象
对类内部实现了隐藏,对类外提供了接口
--------------------------------------------------------------------------------------------------------------------------------
default的使用
String sex; //默认就是一个default权限
不用写default String sex
在相同的包中
package demo1; public class Test { int a = 10; }
package demo1; public class Test2 { public static void main(String[] args) { Test test = new Test(); System.out.println(test.a); } }
不同的类可以进行访问
---------------------------------------------------------------------------------------------------
8.导包
静态导包如下
public class Test3 { public static void main(String[] args) { double x = 30.0; double y = 40.0; /*double result = Math.sqrt(Math.pow(x,2) + Math.pow(y,2));*/ //使用静态导入的方式写起来会更加方便一些 double result = sqrt((pow(x, 2) + pow(y, 2))); System.out.println(result); }
导包
import java.util.Date; import java.util.*;
public static void main1(String[] args) {
Date date = new Date();
System.out.println(date.getTime());
int[] array = {1,2,3,4};
Arrays.toString(array);
}
}
9.static成员
public static String classes = "1077room";
public static void main(String[] args) { Student.classes = "1076rom"; 使用类名来进行访问 public void print(){ System.out.println(this.name + " * " + this.age + "班级" + classes); } public static void fun2(){ this.print(); }//这样会报错的
在静态方法内部,不能直接访问非静态的数据成员和成员方法
类变量存储在方法区当中
10.代码块
普通代码块:就是定义在方法中的代码块
静态代码块
static{ //在类加载时候就被执行 System.out.println("静态代码块"); }
实例代码块(构造代码块)
{ System.out.println("实例代码块"); }
当应用于类中的时候
{ System.out.println("实例代码块"); } static{ //在类加载时候就被执行 System.out.println("静态代码块"); } public Student(String name ,int age){ System.out.println("带两个参数的构造方法"); this.name = name; this.age = age; }
public static void main(String[] args) { Student student = new Student("dfsf",12);
得出的结果是
静态代码块
实例代码块
带两个参数的构造方法
而第二次执行代码的时候,静态的只会执行一次
静态代码块不管生成多少个对象,都只会执行一次
静态成员变量是类的属性,因此是在JVM加载类时开辟空间并初始化的
如果一个类中包含多个静态代码块,在变异代码时,编译器会按照定义的先后次序一次执行
实例代码块只有在创建对象时才会执行
回顾
作业
一。运行结果
public static void main(String[] args) { String s; System.out.println(s); }
在该代码中,String s为局部变量,而局部变量不声明,编译不能通过。则答案为不能通过
二。运行结果
class A{ public static void hello(){ System.out.println("hello"); } } public class Test { public static void main(String[] args) { A a = null; a.hello(); } }
运行结果为hello
在代码A a = null中,他这个代码的意思就是a不指向任何什么东西。
hello是一个静态的,他不依赖对象。
而hello正确的调用本应为A.hello,所以这个a指向什么东西并不重要,所以可以运行成功。
三。
static修饰的是类的方法或变量,而不是方法的局部变量
四。
public class Test { public static void func(){ } public static void main(String[] args) { func(); } }
要是上面不加static就需要这样,初始化后使用
public class Test { public void func(){ } public static void main(String[] args) { Test test = new Test(); test.func(); } }
五。
六。
在方法区中x只有一份嘛
七。
八。
九。
十。
他导包导包,实际上最后导包的是他导的类。
import java.util这个是导包。import java.util.Arrays这个是导完的类。
十一。
使用this()来调用构造方法
十二。
package Homework; import java.util.Scanner; public class Age { public static void main(String[] args) { Person p = new Person(); Scanner scanner = new Scanner(System.in); while(scanner.hasNext()){ int age = scanner.nextInt(); p.setAge(age); System.out.println(p.getAge()); } } } class Person{ private int age;
补全代码,使得小于0时输出0;大于200时输出200,0-200输出改数字
package Homework; import java.util.Scanner; public class Age { public static void main(String[] args) { Person p = new Person(); Scanner scanner = new Scanner(System.in); while(scanner.hasNext()){ int age = scanner.nextInt(); p.setAge(age); System.out.println(p.getAge()); } } } class Person{ private int age; public void setAge(int age) { this.age = age; } public int getAge() { if(this.age < 0){ return 0; } else if (this.age > 200) { return 200; }else{ return this.age; } } }