12.9 Java基础9
- 昨天知识点回顾及作业修改
1.类和对象:
类是模板,根据模板创建出来的实例就是对象.
类是概念性模型,对象就是实际存在的事物。
类是对象的类型,对象是类的实例.
万事万物皆对象,对象集合就是类;
很多个对象,提取公共的属性和和方法,形成类.
2.类的基本语法结构,对象的创建
对象创建 类名 引用名= new 类名();
对象的方法 引用名.方法名 ->非static静态的方法
3.方法的定义和调用
有参数,有返回值 -》常见 return
递归调用 ->先了解-》使用debug去分步查看过程
作业回顾:
/**
* 4、编写一个Java程序,求出1!+2!+3!+4!+5!的和。(5!=5*4*3*2*1)可以使用递归
* @author Administrator
*
*/
public class Test_HomeWork_01 {
/**
* @计算n的阶乘
* @param n
* @return
*/
public int getJieCheng(int n) {
// 从1推
if (n == 1) {
return 1;
}
// 2 getJieCheng(n-1)*n
return getJieCheng(n - 1) * n;
}
}
public class Test_HomeWork_01_Main {
public static void main(String[] args) {
Test_HomeWork_01 th = new Test_HomeWork_01();
int total = 0;
for (int i = 1; i <= 5; i++) {
int result = th.getJieCheng(i);
total += result;
}
System.out.println("total :" + total);
}
}
- 构造器
如果不写构造方法,默认有一个无参数的构造方法。 public Cat() {} 修饰符和类相同
我们自己编码写了一个无参数的构造,会覆盖默认的无参数构造.
如果多个构造方法的参数个数不同,这些方法关系是重载;如果多个构造方法参数个数相同,但是类型不同,也是重载。
this关键字是一个代称。表示的是当前类的对象。例如 this.show1();//调用当前类中的其他实例方法
public TestConstructor() {
System.out.println("无参数的构造方法");
}
public TestConstructor(int a) {
// this()【调用无参构造方法】
// this("小黑",100,100,"公")【调用有参构造方法】
// System.out.println(111);
// 如果调用构造方法,必须是构造方法中的第一条语句,且只用在同一个类中
this();// 调用的是同名的无参数的构造方法
System.out.println("一个参数的构造方法");
}
public static void main(String[] args) {
//创建对象 查看this()是否有调用无参构造方法
new TestConstructor(1);
}
这样的代码也可创建匿名对象调用构造方法,如果调用的无参构造方法里有输出,那么对象每创建一次,无参数的构造方法就调用一次,也就输出几次。
public static void main(String[] args) {
// TODO Auto-generated method stub
//new Dog() 类名() 其实就是隐式的调用无参数的构造方法
//Dog dog = new Dog();
new Dog();//匿名对象 ->依然调用构造方法
new Dog();//匿名对象 ->依然调用构造方法
}
- 代码段运行顺序
public class Dog {
// 属性 [成员属性]
String name;
String color;
/**
* @方法的定义
*/
public void showInfo() {
System.out.println(name + "," + color);
}
// 静态的方法直接通过类名.方法名()调用
public static void show() {
System.out.println("静态方法");
}
// 普通代码段和对象有关,new出来,才执行,而且在对象中优先级高.
// 实例之前会执行,实例一次就执行一次.
{
System.out.println("普通代码段 : " + name + "," + color);
}
// static 这东西和对象无关的,和类紧密关联的. [类所有的.] Dog
// "类加载" 就执行. 比大多数代码都优先。 只执行一次,即加载完毕就不会再次执行
static {
// Cannot make a static reference to the non-static field name
// System.out.println("静态代码段 : "+name+","+color);
System.out.println("static code");
}
// 构造方法特点:
// 无返回类型,方法名和类名一致
public Dog() {
System.out.println("构造方法");
}
}
注意:调用类静态代码段时,必须也要先保证这个类加载JVM。如:
//先了解: 这里将某个类class: day09.Dog加载到JVM
Class.forName("day09.Dog");
- 重载
构造方法的重载:是指同一个类中存在着若干个具有不同参数列表的构造函数。其实我们很早就见过重载的函数(方法),比如说System.out.println(xxx)
// 三个方法要么参数个数不同,要么个数相同,但是类型不同.
//重载的好处:方法名的复用。
System.out.println();
System.out.println("a");
System.out.println(1);
- 全参构造方法
出于安全考虑,我们常将类里的属性私有化,这样其他类就无法对它进行实例化调用。那么如何访问这些属性呢?两种解决方法,其中一个是采用封装里公共的getsetter和setter方法,另一个就是采用全参的构造方法。
例如这样:
public Card(String name, String cardid, String tel, int score, double balance) {
this.name = name;
this.cardid = cardid;
this.tel = tel;
this.score = score;
this.balance = balance;
}
以会员卡为例:
public class Card {
// 属性:姓名、卡号、手机号、积分、余额 [所有属性都私有化了]
private String name;
private String cardid;
private String tel;
private int score;
private double balance;
// 方法:查看姓名、查看卡号、查看手机号、查看积分、查看余额、消费、充值
// 查看姓名
public void showName() {
System.out.println("姓名是:" + this.name);
}
/**
* @带参数构造给属性赋值
* @param name 姓名
* @param cardid 卡号
* @param tel 电话号码
* @param score 积分
* @param balance 余额
*/
public Card(String name, String cardid, String tel, int score, double balance) {
this.name = name;
this.cardid = cardid;
this.tel = tel;
this.score = score;
this.balance = balance;
}
// 查看卡号
public void showCardid() {
System.out.println("卡号是:" + this.cardid);
}
// 查看手机号
public void showTel() {
System.out.println("手机号是: " + this.tel);
}
// 查看积分
public void showScore() {
System.out.println("积分是:" + this.score);
}
// 查看余额
public void showBalance() {
System.out.println("余额是: " + this.balance);
}
/**
* @消费
* @param money 消费的金额
*/
public void consume(double money) {
System.out.println("本次消费金额为:" + money);
if (this.balance > money) {
this.balance -= money;
System.out.println("消费成功,账号成功扣款 " + money + " 元");
System.out.println("账号" + cardid + "的余额为" + this.balance);
} else {
System.out.println("账号" + cardid + "的余额为" + this.balance + ",不足以支付本次消费.");
}
}
/**
* @充值
* @param money 要充值的金额
*/
public void recharge(double money) {
if (money > 0) {
System.out.println("本次充值金额为:" + money);
this.balance += money;
System.out.println("充值成功 ,账号" + cardid + "的余额为" + this.balance);
} else {
System.out.println("充值金额不能为负数.");
}
}
}
- 封装
- 封装的定义:
程序设计追求“高内聚,低耦合”。
高内聚:就是类的内部数据操作细节自己完成,不允许外部干涉。
低耦合:是仅暴露少量的方法给外部使用,尽量方便外部调用。
- 封装的优点:
提高代码的安全性;
提高代码的复用性;
“高内聚”:封装细节,便于修改内部代码,提高可维护性;
“低耦合”:简化外部调用,便于调用者使用,便于扩展和写作。
- 封装的细节
一般使用private访问权限修饰来修饰成员变量。
提供相应的get、set方法来访问相关属性,这些方法通常是public修饰的,以提供对属性的赋值与读取操作。
一些只用于本类的辅助性方法,可以使用private修饰,希望其他类调用的方法用public修饰。
套路步骤:
1.私有化属性
2.生成公共的setter和getter,要进行适当的逻辑判断
3.无参数构造,带参数构造[看情况]
4.toString方法
- 继承(初讲)
概念:所谓继承是指一个类的定义可以基于另外一个已经存在的类,即子类基于父类,从而实现父类代码的重用,子类能吸收已有类的数据属性和行为,并能扩展新的能力。
继承的声明形式:
【访问权限修饰符】【修饰符】子类名 extends 父类名{
子类体
}
- 继承的优点
提高了代码的复用性
提高了代码的维护性:如果功能的代码需要修改,修改一处即可
让类与类之间产生了关系,是多态的前提:其实这也是继承的一个弊端:类的耦合性很强。
- 注意事项:
Java只支持单继承,不过也支持多层继承。
子类只能继承父类所有非私有的成员(成员方法和成员变量)。
子类不能继承父类的构造方法,但是可以通过super(后面讲)关键字去访问父类构造方法。
注意:子类可以去复用父类中的成员[公共属性和公共方法],但是私有的不可以。
样例:public class Pig1 extends Animal{xxx}
- 其他
//vcard为引用,存储是在内存栈中,称为引用或者句柄.
VIPCard vcard = new VIPCard("林","13667896789", "62170019", 100, 500);
//创建对象需要依赖类的存在,类.class一定会加载JVM
//将 String name String tel; balance =>属性值初始化 堆内存,null null 0.0
//表示得出vcard2 和vcard指向的是同一个内存地址
VIPCard vcard2 = vcard;
感想:不难,代码都能敲得出来,只是理论不是那么好弄懂。
18:57:11 2021-12-09
posted on 2021-12-09 18:56 heyiyang1312 阅读(6) 评论(0) 收藏 举报
浙公网安备 33010602011771号