2017年3月24号课堂笔记

2017年3月24号 星期五 雨

内容:

构造方法、static、封装

 Tips:

1、shift+alt+a:列编辑模式,可方便的同时操作几列数据

 原图:(同大小比对)

使用后效果如下图:(同大小比对)

2、shift +alt+ s +o :快速生成构造方法

1)有参方法:内部选项勾选,全选快捷键为Alt+a

2)无参方法:不勾选

3、shift +alt +s +r: 快速生成类中所有成员变量的get和set方法

一、老师写的内容文档:

构造方法:

01.无参构造 访问修饰符 类名(){ }
02.带参构造 访问修饰符 类名(参数1,参数2...){}
03.没有返回值,不能写void


public class Student{

// 这是一个普通的方法
public void Student(){

}
}

static:


如果在一个成员变量或者方法前 加上了static修饰符
那么这个方法和变量我们称之为全局方法/全局变量!(静态方法/静态变量)

static修饰符的变量 属于类!
所有的对象都共享这一个变量!
一个对象改变这个属性的值,会影响其他对象的属性!

static修饰的属性或者方法
我们访问的时候 可以直接使用 类名.属性 类名.方法名()

01.static修饰的方法(静态方法)不能访问非静态方法
02.非静态方法可以访问静态方法!
03.静态方法中不能访问非静态的变量!

final int num
01.写在方法中 可以不赋值
02.写在类中 必须赋值
static final int num;(静态常量)必须赋予初始值 否则编译报错!

创建对象后的执行顺序:

* 01.创建对象

* 02.在类被加载的时候先去 加载 static修饰的属性以及方法,代码块(代码块优先于方法加载)

* 03.之后执行普通代码块

* 04.最后执行构造方法

二、Demo01(宠物领养)构造方法Demo

1、老师代码:

1)狗狗实体类

package cn.bdqn.bean;

/**
*狗狗的实体类
*/
public class Dog {
// 成员变量
public String name; // 姓名
public int health;// 健康值
public int love;// 亲密度
public String strain; // 品种

/**
* 无参构造方法
* 访问权限修饰符 类名(){
* }
*/
public Dog() {
System.out.println("Dog类的无参构造函数!");
}

/**
* 带参构造函数
* 用户在创建对象的时候,给我们传递过来什么值,我们就给数值赋什么值
*/
public Dog(String name, String strain, int health, int love) {
System.out.println("Dog类的带参构造函数!");
this.name = name;
this.strain = strain;
this.health = health;
this.love = love;
}

/**
* 输出狗狗的信息
*/
public void showInfo() {
System.out.println("姓名:" + this.name);
System.out.println("品种:" + this.strain);
System.out.println("健康值:" + this.health);
System.out.println("亲密度:" + this.love);
}

/**
* 测试方法
* 测试 我们能不能在实例化对象的同时,给对象的各个属性赋值!
*/
public static void main(String[] args) {
/**
* 会默认执行Dog类中的无参构造函数!
* 如果类中没有对应的无参构造函数,系统默认会创建一个!
* Dog dog = new Dog();
dog.showInfo();
存在的问题:
如果我们实例化了N个狗狗对象!
但是每个对象的属性值都是一致的!

如果我们显式的在类中创建了带参构造,那么系统就不会给我们创建无参构造!
*/
Dog dog = new Dog();
dog.showInfo();
}
}

2)企鹅实体类

package cn.bdqn.bean;

/**
*企鹅的实体类
*/
public class Penguin {
// 成员变量
public String name; // 姓名
public int health;// 健康值
public int love;// 亲密度
public String sex; // 性别

/**
* 输出企鹅的信息
*/
public void showInfo() {
System.out.println("姓名:" + this.name);
System.out.println("性别:" + this.sex);
System.out.println("健康值:" + this.health);
System.out.println("亲密度:" + this.love);

}

// shift +alt+ s +o
public Penguin(String name, int health, int love, String sex) {
this.name = name;
this.health = health;
this.love = love;
this.sex = sex;
}

// 无参构造
public Penguin() {

}

public Penguin(String name, int health) {
this.name = name;
this.health = health;
}

public Penguin(String name, int health, String sex) {
this.name = name;
this.health = health;
this.sex = sex;
}

// 构造的重载
public static void main(String[] args) {
Penguin penguin = new Penguin("小企鹅", 100, "Q仔");
penguin.showInfo();

}
}

3)宠物测试类

package cn.bdqn.test;

import java.util.Scanner;

import cn.bdqn.bean.Dog;
import cn.bdqn.bean.Penguin;

public class PetTest {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("*************欢迎来到宠物商店*************");
System.out.println("请输入领养宠物的名称:");
String name = scanner.next();
System.out.println("请输入领养宠物的类型:(1:狗狗 2:企鹅)");
int choose = scanner.nextInt();
switch (choose) {
case 1: // 狗狗
System.out.println("请选择狗狗的品种:(1:藏獒 2:拉布拉多犬)");
choose = scanner.nextInt();
Dog dog = new Dog(); // 创建出一个狗狗的实例
if (choose == 1) {
dog.strain = "藏獒";
} else {
dog.strain = "拉布拉多犬";
}
dog.name = name;
// 调用自我介绍的方法
dog.showInfo();
break;
case 2: // 企鹅
System.out.println("请选择企鹅的性别:(1:Q仔 2:Q妹)");
choose = scanner.nextInt();
Penguin p = new Penguin(); // 创建出一个企鹅的实例
if (choose == 1) {
p.sex = "Q仔";
} else {
p.sex = "Q妹";
}
p.name = name;
// 调用自我介绍的方法
p.showInfo();
break;
}

}
}

2、自己代码:

1)狗狗实体类

package cn.bdqn.bean;

/**
* 狗狗实体类
*<P>Title<P>Dog
*<P>Description<P>
* @author alex
* @date 2017-3-24上午9:31:56
* You can either travel or read,but either your body or soul must be on the way
*/
public class Dog {
// 成员变量
public String name;// 姓名
public String strain;// 品种
public int health;// 健康值
public int love;// 亲密度

// 无参构造
public Dog() {
super();
}

// 带参构造
public Dog(String name, String strain, int health, int love) {
super();
this.name = name;
this.strain = strain;
this.health = health;
this.love = love;
}

/**
* 输出狗狗信息
*/
public void show() {
System.out.println("姓名:" + this.name);
System.out.println("品种:" + this.strain);
System.out.println("健康值:" + this.health);
System.out.println("亲密度:" + this.love);
}

/**
* 测试方法
* 测试能否在实例化对象同时,给对象各属性赋值
*/
public static void main(String[] args) {
Dog dog = new Dog("赤丸", "忍犬", 100, 100);
dog.show();

}
}

2)企鹅实体类:

package cn.bdqn.bean;

/**
* 企鹅的实体类
*<P>Title<P>Penguin
*<P>Description<P>
* @author alex
* @date 2017-3-24上午9:41:29
* You can either travel or read,but either your body or soul must be on the way
*/
public class Penguin {
public String name;
public String sex;
public int health;
public int love;

/**
* 输出企鹅的信息
*/
public void show() {
System.out.println("名字:" + this.name);
System.out.println("性别:" + this.sex);
System.out.println("健康值:" + this.health);
System.out.println("亲密度:" + this.love);
}

/**
*shift+alt+s--->o--->选择select or deselect创建带参构造方法或无参构造方法
*/
public Penguin(String name, String sex, int health, int love) {
super();
this.name = name;
this.sex = sex;
this.health = health;
this.love = love;
}

public Penguin() {
super();
}

/**
* 测试方法
* 分别创建两个对象测试无参构造方法和带参构造方法
*/
public static void main(String[] args) {
Penguin p1 = new Penguin();
p1.show();
System.out.println("**************************");
Penguin p2 = new Penguin("帝企鹅", "雄企鹅", 80, 99);
p2.show();
}
}

3)宠物测试类:

package cn.bdqn.test;

/**
* 宠物测试类
*/
import java.util.Scanner;

import cn.bdqn.bean.Dog;
import cn.bdqn.bean.Penguin;

public class PetTest {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("欢迎来到宠物商店");
System.out.println("请输入领养宠物的名称:");
String name = input.next();
System.out.println("请输入领养宠物的类型:1、狗狗 2、企鹅");
int choose = input.nextInt();
switch (choose) {
case 1:// 狗狗
System.out.println("请选择领养狗狗的品种:1、泰迪 2、阿拉斯加");
choose = input.nextInt();
Dog dog = new Dog();// 创建一个狗狗的实例
if (choose == 1) {
dog.strain = "泰迪";
} else {
dog.strain = "阿拉斯加";
dog.name = name;
// 调用狗狗自我介绍方法
dog.show();
break;
}

case 2:// 企鹅
System.out.println("请选择领养企鹅的性别:1、雄企鹅 2、雌企鹅");
choose = input.nextInt();
Penguin p = new Penguin();// 创建一个企鹅的实例
if (choose == 1) {
p.sex = "雄企鹅";
} else {
p.sex = "雌企鹅";
}
p.name = name;
// 调用企鹅自我介绍方法
p.show();
break;
}
}
}

三、Demo02(static各种案例)

 老师代码:

1)成员变量与全局变量区别:

package cn.bdqn.statics;

/**
*
public class Student {
name是成员变量:所有的对象都能访问的属性!但是每个对象都是独一无二的!
所以说:一个对象改变这个属性的值,不会影响其他对象的属性!
String name;

public static void main(String[] args) {
Student stu1 = new Student();
stu1.name = "小黑";
System.out.println("stu1的name属性值是:" + stu1.name);

Student stu2 = new Student();
stu2.name = "小黑2";
System.out.println("stu2的name属性值是:" + stu2.name);
}
}
*/

public class Student {
/**
* 如果在一个成员变量或者方法前 加上了static修饰符
* 那么这个方法和变量我们称之为全局方法/全局变量!
*
* static修饰符的变量 属于类!
* 所以的对象都共享这一个变量!
* 一个对象改变这个属性的值,会影响其他对象的属性!
*
*/
static String name;

public static void main(String[] args) {
Student stu1 = new Student();
stu1.name = "小黑";
System.out.println("stu1的name属性值是:" + stu1.name);

Student stu2 = new Student(); // 明明没有给stu2的name属性赋值,但是name确实有值
System.out.println("stu2的name属性值是:" + stu2.name);
}
}

2)老师投票:

package cn.bdqn.statics;

/**
* 投票!
* 01.我们自定义总票数
* 02.每个人投一票,那么总票数-1
*/
public class Teacher {
/**
* static修饰的属性或者方法
* 我们访问的时候 可以直接使用 类名.属性 类名.方法名()
*/
static int count = 100; // 总票数

// 投票的方法
public void mCount() {
System.out.println("当前的票数是:" + (--count));
}

public static void main(String[] args) {
Teacher tea1 = new Teacher(); // 创建第1个老师对象
Teacher tea2 = new Teacher(); // 创建第2个老师对象
Teacher tea3 = new Teacher(); // 创建第3个老师对象
// 开始投票
tea1.mCount();
tea2.mCount();
Teacher.count = 1;
tea3.mCount(); // 0
}

}

3)静态方法与非静态方法

package cn.bdqn.statics;

public class TeacherTest {
static int age;

/**
* 01.static修饰的方法(静态方法)不能访问非静态方法
* 02.非静态方法可以访问静态方法!
* 03.静态方法中不能访问非静态的变量!
*/
public static void main(String[] args) {
getName("alex");//Cannot make a static reference to the non-static method getName(String) from the type TeacherTest
System.out.println();
}

/**
* @param name 用户传递过来的参数
* @return 返回给用户的值
*/
public String getName(String name) {
getAge();
return name + "哈哈";
}

public static void getAge() {
System.out.println("您的年龄:" + age);

}

}

4)static修饰代码块时候的执行顺序

package cn.bdqn.statics;

public class StaticTest {

static String name;

public StaticTest() {
System.out.println("无参构造函数");
}

{
System.out.println("普通代码块1");
}

{
System.out.println("普通代码块2");
}

static {
System.out.println("静态代码块1");
}

static {
System.out.println("静态代码块2");
}

public static void sayHello() {
System.out.println("sayHello");
}

public static void main(String[] args) {

/**StaticTest test = new StaticTest(); // 结果??
* new StaticTest()
* 01.创建对象
* 02.在类被加载的时候先去 加载 static修饰的属性以及方法,代码块(代码块优先于方法加载)
* 03.之后执行普通代码块
* 04.最后执行构造方法
*/

StaticTest.sayHello();
}

}

四、Demo03(封装的文档笔记和举例)

 老师代码:

1)实体类:

package cn.bdqn.bean;

/**
*
* @author 小豆腐
*面向对象 3大特性:
*01.封装 :
*将类内部的信息隐藏起来!不允许外部直接的访问!而是通过类中提供的方法来访问!
*
* 封装的好处:
* 01.隐藏类内部的细节信息
* 02.方便我们加入控制语句
* 03.只能通过我们指定的方法来访问我们的数据
* 怎么实现封装?
* 01.所有的属性私有化
* 02.创建对应的set(赋值)和get(取值)方法
* 03.在对应的set方法中 增加逻辑判断!
*
*02.继承
*03.多态
*/
public class Student {

// 01.所有的属性私有化
private String name;
private int age;

// shift +alt +s +r 快速生成类中所有成员变量的get和set方法
// 赋值操作
public void setAge(int age) {
this.age = age;
if (age < 0 || age > 200) {
System.out.println("您的年龄去火星吧!");
this.age = 20;
}
}

public int getAge() {
return this.age;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

}

2)测试类:

package cn.bdqn.test;

import cn.bdqn.bean.Student;

public class StudentTest {

public static void main(String[] args) {
Student stu1 = new Student();
stu1.setAge(25);
System.out.println("您的年龄是:" + stu1.getAge());
}

}

五、static修饰与非static修饰的区别(图解)

六、备注

今天迟到了,以后下雨天要提前出发半小时(以免赶不上车耽误上课)

七、作业

1、视频看到接口,做笔记

笔记示例如下:

2、L课程老师这周讲的自己都练习敲一遍,有余力的话预习下节课要讲的(百度)

3、做题

八、考试

2017.03.22
15:10 开始,15:50结束;答题时间:39分钟;检查时间:1分钟;
成绩:84分

今天状态不好(分数是进入面向对象模块的最低分),回去好好调整,要多做题目,没有题目做了就往下多看视频或者做错题,这样才能尽快提高!

九、老师辛苦了!

 

原文地址:https://www.cnblogs.com/wsnedved2017/p/6611235.html