this关键字static关键字代码块Package--导包----import封装#上机练习:上机练习1——设计Dog和Penguin类 ▪ 需求说明: – 运用面向对象思想抽象出Dog类和Penguin类,
this关键字this关键字概述:表示当前对象指针,指向当前对象,表示当前对象的引用。 this关键字的用途: 普通方法: 区分类成员属性和方法的形参. – 调用当前对象的其他方法(可以省略) 位置:任意 构造方法: 使用this来调用其他方法 位置:必须是第一条语句
This的测试代码
package oop2;
public class TestThis {
int a,b,c;
TestThis(){
System.out.println("正要new一个Hello对象");
}
TestThis(int a,int b){
this();//调用无参的构造方法,并且必须位于第一行
a=a;//这里是指的是局部变量而不是成员变量
this.a = a;
this.b = b;//这样就区分成员变量和局部变量
}
TestThis(int a,int b,int c){
this(a,b);
this.c =c;
}
void show(){
}
void chifan(){
this.show();
System.out.println("你妈妈喊你回家吃饭");
}
public static void main(String[] args) {
TestThis s1 =new TestThis();
s1.chifan();
}
}
static关键字
修饰成员变量的时候,叫做静态变量或者类变量
普通变量在使用的时候,必须通过对象名进行调用
类变量和静态变量在使用时可以使用对象名进行调用,也可以使用类名进行调用
修饰方法时叫做静态方法或者类方法
普通方法在使用的时候,必须通过对象名进行调用
调用静态方法是可以用类名进行调用,也可以使用方法调用
static关键字不能使用this关键字
static关键字静态属性的访问形式 (1)对象名.属性
(2)类名.属性静态方法访问修饰符 static 返回值类型 方法名(){}访问形式
(1)对象名.方法名(); (2)类名.方法名();
package oop2;
public class StaticDemo {
int a;
static int width;
static void gg(){
System.out.println("gg ");
}
void tt (){
System.out.println("tt");
}
public static void main(String[] args) {
StaticDemo s1 = new StaticDemo();
StaticDemo.width = 2;
StaticDemo.gg();//gg();
//通过引用也可以访问static变量或者static方法,不过,一般使用类名.static成员名来访问
s1.tt();
}
}
//当是静态属性时,可以在一个空间里创造三个对象
//当不是静态属性时,他们在同一个空间里创造三个不同的对象
public class StaticDemo2
//static int count 静态属性
int count;//非静态属性
public StaticDemo2(){
//无参构造方法
count++;
System.out.println("创建了"+count+"个对象");
}
public static void main(String[] args) {
new StaticDemo();//创建匿名对象
new StaticDemo();
new StaticDemo();
}
}
Ststic修饰和非Static修饰的区别
属性:前者为类属性、类变量,后者为实例属性、实例变量
方法:前者为类方法,后者为实例方法
调用方法:前者多出一个类名.属性、类名.方法,后者为对象.属性、对象.方法
归属:前者为类,后者为单个对象
代码块:使用{}括起来的一段代码叫做代码块
分类:
普通代码块: 在定义方法时,使用{}括起来的代码叫做普通代码块
构造代码块: 在定义类时,使用{}括起来的代码块叫做构造代码块
注意:构造代码块的代码会添加到每一个构造方法中,使用this可以避免
静态代码快: 使用static{}括起来的代码块叫做静态代码块
同步代码块: 在使用多线程的时候会用到
执行顺序: 静态代码快>构造代码块>普通代码块
public class CodeBlockDemo {
int x;
int y;
public CodeBlockDemo(int x,int y){
{
System.out.println("构造代码块执行");
}
System.out.println(" CodeBlockDemo(int x,int y)被执行");
this.x = x;
this.y = y;
}
//构造代码块: 在定义类时,使用{}括起来的代码块叫做构造代码块
{
System.out.println("构造代码块被执行");
}
//静态代码快: 使用static{}括起来的代码块叫做静态代码块
//static代码块优先级最高
static {
System.out.println("static代码块被执行了");
}
// 普通代码块: 在定义方法时,使用{}括起来的代码叫做普通代码块
public void test1(){
System.out.println("test1方法被执行");
{
System.out.println("赵云");
}
}
public static void main(String[] args) {
// CodeBlockDemo cbd = new CodeBlockDemo();
//cbd.test1();
{
System.out.println("main方法被执行");
}
CodeBlockDemo cbd2 = new CodeBlockDemo(1,4);
cbd2.test1();
}
}
Package–
包通常是类的第一句非注释性语句。 包名:域名倒着写即可,再加上模块名,并不内部管理类。
java.lang
包含一些Java语言的核心类,如String、Math、Integer、System和Thread,提供常用功能。
java.awt
包含了构成抽象窗口工具集(abstract window toolkits)的多个类,这些类被用来构建和管理应用程序的图
形用户界面(GUI)。
java.net
包含执行不网络相关的操作的类。
java.io
包含能提供多种输入/输出功能的类。
java.util
包含一些实用工具类,如定义系统特性、使用不日期日历相关的函数
如果使用其他包中的方法,我们就需要导包
java会默认导入java.lang包下所有的类,因此这些类我们可以直接使用
使用方式:
import java.util.Date;
import java.util
在使用Idea工具时会自动进行导包
面向对象的三大特征:继承,多态,封装。
封装的要点:
类的属性的处理:
1.一般使用private. (除非本属性确定会让子类继承)
2.提供相应的get/set方法来访问相关属性. 这些方法通常是public
,从而提供对属性的读取操作。 (注意:boolean变量的get方法是
用:is开头!)
class Dog { Dog
private String name = "旺财"; // 昵称
private int health = 100; // 健康值
private int love = 0; // 亲密度
private String strain = "拉布拉多犬"; // 品种
public int getHealth() {
return health;
}
public void setHealth (int health) {
if (health > 100 || health < 0) {
this.health = 40;
this代表 System.out.println("健康值应该在0和100之间,默认值是40");
else
this.health = health;
当前对象
}
// 其它getter/setter方法
}
#上机练习:上机练习1——设计Dog和Penguin类 ▪ 需求说明: – 运用面向对象思想抽象出Dog类和Penguin类,
类型
狗
属性 行为
昵称 健康值 亲密度 品种
昵称 健康值 亲密度 性别
输出信息
企鹅 输出信息
– 根据类图编写Dog类和Penguin类 – 添加默认构造方法,最后在控制台输出
创建dog类
public class Dog {
private String name;
private int health;
private int love;
private String gender;
//创建构造器
public Dog() {
}
public Dog(String name, int health, int love, String gender) {
this.name = name;
this.health = health;
this.love = love;
this.gender = gender;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getHealth() {
return health;
}
public void setHealth(int health) {
this.health = health;
}
public int getLove() {
return love;
}
public void setLove(int love) {
this.love = love;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public void show() {
System.out.println("狗的昵称是" + this.name + "狗的健康值是" + this.health+ "狗的亲密度是" + this.love+ "狗的品种是" + this.gender);
}
}
创建企鹅类
package oop2;
public class Penguin {
private String name;
private int health;
private int love;
private String gender;
public Penguin() {
}
public Penguin(String name, int health, int love, String gender) {
this.name = name;
this.health = health;
this.love = love;
this.gender = gender;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getHealth() {
return health;
}
public void setHealth(int health) {
this.health = health;
}
public int getLove() {
return love;
}
public void setLove(int love) {
this.love = love;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public void show2(){
System.out.println("企鹅昵称是" + this.name + "企鹅的健康值是" + this.health + "企鹅的亲密度是" + this.love+ "企鹅的亲密度是" + this.gender);
}
}
创建测试类
package oop2;
public class TestAnimal {
public static void main(String[] args) {
Dog dog = new Dog("小黑",80,88,"公的");
dog.show();
Penguin penguin = new Penguin("小白",80,90,"母的");
penguin.show2();
}
}



