标题001.构造方法(Constructor)
构造方法的作用
给对象的数据和属性进行初始化
构造方法的格式特点
方法名与类名相同(大小写也一致)
没有返回值类型
没有具体的返回值return;
package hyq.day07;
/**
* @Author: 强啊
* @CreateTime: 2018-12-10 19:42
*/
public class day07demo1Constructor { //Constructor构造;
public static void main(String[] args){
person p = new person(); //构造方法在创建对象的时候,系统就自动调用了构造方法
//p.person; // 构造方法不能用对象调用
p.show();
}
}
class person{
private String name;
private int age;
//构造方法
public person(){
//System.out.println("Hello world");
//return ; //构造方法也是有return语句的
name = "张三";
age = 23;
}
public void show(){
System.out.println(name + "...." + age);
}
}
标题002构造方法的重载以及注意事项
1.重载:方法相同,与返回值类型无关(构造方法没有返回值),只看参数列表。
2.如果我们没有定义构造方法,系统将自动提供一个无参构造方法
3.如果我们定义了构造方法,系统将不再提供默认的构造方法。如果这个时候我们想使用无参构造方法,就必须自己给出。
package hyq.day07;
/**
* @Author: 强啊
* @CreateTime: 2018-12-10 20:10
*/
/*A:案例演示
*构造方法的重载
*重载:方法名相同,与返回值类型无关(构造方法没有返回值),只看参数列表
*B:构造方法注意事项
*a:如果我们没有给出构造方法,系统将自动提供一个无参构造方法。
*b:如果我们给出了构造方法,系统将不再提供默认的无参构造方法。
*注意:这个时候,如果我们还想使用无参构造方法,就必须自己给出。建议永远自己给出无参构造方法*/
public class day07demo2 {
public static void main(String[] args) {
person2 p2 = new person2();
p2.show();
System.out.println("-----------------------");
person2 p3 = new person2("张三", 230);
p3.show();
person2 p4 = new person2("李四", 24);
p4.show();
}
}
class person2 {
private String name;
private int age;
public person2() {
System.out.println("空参构造方法");
}
public person2(String name, int age) {
this.name = name;
this.age = age;
System.out.println("有参数的构造方法");
}
public void show() {
System.out.println(name + "..." + age);
}
}
有参构造的好处:可以动态的给参数进行赋值
003
setXxx()方法:修改属性值 ,此方法更为灵活,在开发中多用
构造方法:给对象中的属性进行初始化
package hyq.day07;
/**
* @Author: 强啊
* @CreateTime: 2018-12-10 20:29
*/
public class day07demo3 {
public static void main(String[] args) {
person3 p = new person3("张三",23); //有参构造创建对象
System.out.println(p.getName() + "-------" + p.getAge());
System.out.println(p);
System.out.println("---------------------");
p = new person3("张四",23); //这不是改名,是“杀对象”。(将原来的对象变成垃圾)
System.out.println(p.getName() + "-------" + p.getAge());
System.out.println(p);
System.out.println("---------------------------");
person3 p1 = new person3(); //空参构造创建对象
p1.setName("李四");
p1.setAge(24);
System.out.println(p1.getName() + "---------" + p1.getAge());
System.out.println(p1);
}
}
class person3 {
private String name; //姓名
private int age; //年龄
public person3() { //空参构造
}
public person3(String name, int age) { //有参构造
this.name = name;
this.age = age;
}
public void setName(String name) { //设置姓名
this.name = name;
}
public String getName() { //获取姓名
return name;
}
public void setAge(int age) { //设置年龄
this.age = age;
}
public int getAge() { //获取年龄
return age;
}
}
004一个标准学生类的代码及测试
A:案例演示
完善一下我们的学生的类
B:给成员变量赋值:
a:setXxx()方法
b:构造方法
C:输出成员变量值的方式:
a:通过getXxx()分别获取然后拼接
b:通过调用show()方法搞定
package hyq.day07;
/**
* @Author: 强啊
* @CreateTime: 2018-12-11 00:20
*/
public class day07demo4student {
public static void main(String[] args) {
student s1 = new student(); //使用无参构造方法创建对象
s1.setName("藏三"); //设置姓名
s1.setAge(23); //设置年龄
System.out.println("姓名是 : " + s1.getName() + ",年龄是 : " + s1.getAge());
//可以获取属性值,可以将其打印,赋值给其他变量,也可作其他操作。
student s2 = new student("李四", 24); //使用有参构造函数创建对象
s2.show(); //仅仅显示属性值
}
}
class student{
private String name;
private int age;
//Alt+insert 快捷键创建构造方法与get(set)方法
public student(){ //空参构造
}
public student(String name, int age){ //有参构造
this.name = name;
this.age = age;
}
public void setName(String name){ //设置姓名
this.name = name;
}
public String getName(){ //获取姓名
return name;
}
public void setAge(int age){ //设置年龄
this.age = age;
}
public int getAge(){ //获取年龄
return age;
}
public void show(){ //显示姓名年龄方法
System.out.println("姓名是 : " + name + ",年龄是 : " + age );
}
}
005创建一个对象的步骤
demo:Student s = new Student();
1.Student.class 加载进内存
2.声明一个Student类型引用 : Student s
3.在堆内存中创建对象 :new Student
4.给对象属性默认初始化值
5.属性进行显示初始化
6.构造方法进栈,对对象中的属性进行属性赋值,构造方法弹栈
7.将对象的地址值赋值给s
006矩形类
package hyq.day07;
/**
* @Author: 强啊
* @CreateTime: 2018-12-11 14:30
*/
public class day07demo5Rectangle {
public static void main(String[] args) {
Rectangle r = new Rectangle(10,20);
System.out.println("该矩形周长是:" + r.getLength());
System.out.println("该矩形面积是:" + r.getArea());
}
}
/*
A:案例演示
*需求:
*定义一个长方形类,定义 求周长和面积的方法,
*然后定义一个测试类进行测试。
分析:
成员变量:
宽:width,高high
空参有参构造
成员方法:
set,get。
求周长:getLenth()
求面积:getArea()
*/
class Rectangle{
private int width;
private int high;
public Rectangle(){
}
public Rectangle(int width,int high){
this.width = width;
this.high = high;
}
public void setWidth(int width){
this.width = width;
}
public int getWidth(){
return width;
}
public void setHigh(int high){
this.high = high;
}
public int getHigh(){
return high;
}
public int getLength() {
return 2 * (width + high);
}
public int getArea() {
return width * high;
}
}
006面向对象(员工类案例练习)
A:案例演示
* 需求:定义一个员工类Employee
* 自己分析出几个成员,然后给出成员变量
* 姓名name,工号id,工资salary
* 构造方法,
* 空参和有参的
* getXxx()setXxx()方法,
* 以及一个显示所有成员信息的方法。并测试。
package hyq.day07;
import java.util.PrimitiveIterator;
/**
* @Author: 强啊
* @CreateTime: 2018-12-11 14:52
*/
public class day07demo6Empliyee {
public static void main(String[] args) {
Employee e = new Employee("百里空城","111111",99999.99 );
e.show();
}
}
class Employee{
private String name;
private String id;
private double salary;
public Employee(){
}
public Employee(String name,String id,double salary){
this.name = name;
this.id = id;
this.salary = salary;
}
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
public void setId(String id){
this.id = id;
}
public String getId(){
return id;
}
public void setSalary(double salary){
this.salary = salary;
}
public double getSalary(){
return salary;
}
public void show(){
System.out.println("姓名:" + name + ",id:" + id + "工资是" + salary);
}
}
007面向对象(static关键字及内存图)
- 通过一个案例引入static关键字。
- 人类:Person。每个人都有国籍,
008static关键字的特点
1.随着类的加载而加载
2.优先于对象存在
3.被类的所有对象共享
4.可以通过类名调用
5.也可以通过对象调用
6.推荐使用类名调用
009static 注意事项
1.在静态方法中没有this关键字的
a.静态随着类的加载而加载的,this是随对象的创建而存在的
b.静态比对象先存在
2.静态方法只能访问静态的成员变量和静态的成员方法
静态方法:
成员变量:只能访问静态变量
成员方法:只能访问静态成员方法
非静态方法:
成员变量:可以是静态的也可以是非静态的
成员方法:可以是静态也可以是非静态的
静态只能访问静态
010静态变量与成员变量的区别
静态变量也叫类变量,成员变量也叫对象变量
1.所属不同
静态变量属于类,所以也称为类变量
成员变量属于对象,也称实例变量(对象变量)
2.内存中的位置不同
静态变量存储与方法区的静态区
成员变量存储于堆内存
3.内存中出现的时间不同
静态变量随着类的加载而加载,随着类的消失而消失
成员变量随着对象的创建而存在,随着对象的消失而消失
4.调用不同
静态变量可以通过类名调用,也可以通过对象调用
成员变量只能通过对象名调用
011main方法的格式详细解释
1.格式:
public static void main(String[] srgs) { }
2.对格式的解释
public 被jvm调用,访问权限足够大
static 被jvm调用 ,不用创建对象,直接类名访问
void 被jvm调用,不需要给jvm返回值
main一个通用的名称,虽然不是关键字,但是被jvm识别
String[] args 以前用于键盘录入的
012* A:制作一个工具类
* ArrayTool
* 1,获取最大值
* 2,数组的遍历
* 3,数组的反转
//数组工具类
package hyq.day07;
/**
* @Author: 强啊
* @CreateTime: 2018-12-11 22:20
*/
public class day07demo8ArrayTool {
//*ArrayTool
//*1,获取最大值
public int getMax(int[] arr){
int max = arr[0];
for (int i = 1; i < arr.length; i++) {
if(max < arr[i]){
max = arr[i];
}
}
return max;
}
//*2,数组的遍历
public void print(int[] arr){
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
//*3,数组的反转
public void revArr(int[] arr){
for (int i = 0; i < arr.length/2; i++) {
int temp = arr[i];
arr[i] = arr[arr.length - 1 -i];
arr[arr.length - 1 - i] = temp;
}
}
}
测试类
package hyq.day07;
/**
* @Author: 强啊
* @CreateTime: 2018-12-11 22:20
*/
public class day07demo801 {
public static void main(String[] args){
int[] arr = {11,22,3,65,48};
day07demo8ArrayTool da = new day07demo8ArrayTool();
int max = da.getMax(arr);
System.out.println(max);
System.out.println();
System.out.println("---------");
da.print(arr);
System.out.println("----------------");
da.revArr(arr);
da.print(arr);
}
}
package hyq.day07;
/**
* @Author: 强啊
* @CreateTime: 2018-12-11 22:20
*/
public class day07demo8ArrayTool {
//如果一个类中所有的方法都是静态的,那么需要私有构造方法,目的是不让
//其他类创建本类对象,直接用类名.调用
private day07demo8ArrayTool(){
}
//*ArrayTool
//*1,获取最大值
public static int getMax(int[] arr){
int max = arr[0];
for (int i = 1; i < arr.length; i++) {
if(max < arr[i]){
max = arr[i];
}
}
return max;
}
//*2,数组的遍历
public static void print(int[] arr){
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
//*3,数组的反转
public static void revArr(int[] arr){
for (int i = 0; i < arr.length/2; i++) {
int temp = arr[i];
arr[i] = arr[arr.length - 1 -i];
arr[arr.length - 1 - i] = temp;
}
}
}