栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java日记(java进阶)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java日记(java进阶)

回顾

万物皆对象

学习程序的目的:解决现实的问题。

前提:模拟现实世界

小明(人) – 人类

public class Human {

    String name;
    String gender;
    int height;
    int weight;
}
public class Bear {

    
}
public class Computer {

    
}
Human man = new Human();
man.name = "小明";
man.gender = "男";
man.height = 168;
man.weight = 160;

实例变量和局部变量的区别:

方法的重载:

方法的签名(唯一性)在一个类中,方法名相同,参数列表不同就叫做方法的重载 今日内容 1. 构造方法

无参构造方法

没有参数 有参构造方法

有参数

定义:

public 类名() {

}

public 类名(参数列表) {

}

构造方法构造方法,不能被直接调用,只能在创建对象时调用(new)如果不自己定义一个构造器,那么系统就默认有一个无参构造方法

一旦自己定义一个构造器(无参,有参都可),那么就没有默认的无参构造方法了

package com.qf;


public class Demo01 {

	public static void main(String[] args) {
		Person p = new Person();
		
		System.out.println(p);
		
		//表示自己
		Student stu = new Student();
		stu.name = "小帅哥";
		stu.code = "888888";
		stu.gender = "男";
		stu.cardId = "666999";
		
		
		Student stu2 = new Student("jackma","9999","男","123456");
		
		
		String name = stu2.name;
		String code = stu2.code;
		String gender = stu2.gender;
		String cardId = stu2.cardId;
		System.out.println("name:"+name+" code:"+code+" gender:"+gender+" cardId:"+cardId);
		
		
		System.out.println("----------------------------");
		
		
		Computer c = new Computer("华为",5600,"i5","8G");
		
	}
	
}

class Person {
	
	
	public Person() {
		System.out.println("执行Person无参构造方法");
	}
	
}

class Student {
	String name;
	String code;
	String gender;
	String cardId;
	
	
	public Student() {
		
	}
	
	
	public Student(String n, String c, String g, String id) {
		System.out.println("执行有参构造方法--");
		
		name = n;
		code = c;
		gender = g;
		cardId = id;
	}
	
}


class Computer {
	//定义四个属性   品牌  价格 cpu  内存
	String brand;
	double price;
	String cpu;
	String ram;
	
	
	public Computer(String b, double p, String c, String r) {
		brand = b;
		price = p;
		cpu = c;
		ram = r;
	}
	
}
2. this

在有参构造器中,实例变量和局部变量可能会相同

在相同的作用范围中,会优先使用局部变量

可以使用this关键字使用类中的全局变量

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Z29dFWej-1646829039517)(image-20220309144026907.png)]

package com.qf;

public class Demo02 {

	public static void main(String[] args) {
		
	}
}

class Phone {
	
	String brand;
	double price;
	String cpu;
	String ram;

	
	public Phone(String brand, double price, String cpu, String ram) {
		this.brand = brand;
		this.price = price;
		this.cpu = cpu;
		this.ram = ram;
	}
	
	public Phone(String brand, double price) {
		this.brand = brand;
		this.price = price;
	}

	public Phone(String brand, double price, String cpu) {
		this.brand = brand;
		this.price = price;
		this.cpu = cpu;
	}

	public Phone(String brand) {
		this.brand = brand;
	}
	
	
	public Phone() {
		
	}
	
	
}

class Cup {
	
	String brand;
	double price;
	String name;
	int capacity;
	
	public Cup(String brand) {
		super();
		this.brand = brand;
	}

	public Cup(double price) {
		super();
		this.price = price;
	}

	public Cup(int capacity) {
		super();
		this.capacity = capacity;
	}

	public Cup(String brand, double price) {
		super();
		this.brand = brand;
		this.price = price;
	}

	public Cup(String brand, String name) {
		super();
		this.brand = brand;
		this.name = name;
	}

	public Cup(String brand, int capacity) {
		super();
		this.brand = brand;
		this.capacity = capacity;
	}
	
}
3. 面向对象特征

抽象,封装,继承,多态

抽象 抽取一个事情重要的特性封装 重要的东西,只能自己用,别人不能获取 属性的私有化继承 类别与类别之间的关系多态 相同的种类,执行相同的操作,得到的结果却不一样 3.1 封装

属性私有为私有属性,添加set,get方法

package com.qf;

public class Person {

	private String name;
	private String gender;
	
	private int age;
	
	public Person() {
		
	}
	
	//1.1 定义有参构造器,为属性赋值
	public Person(int age) {
		this.age = age;
	}
	
	//1.2 通过实例方法赋值
	public void setAge(int age) {
		this.age = age;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	public void setGender(String gender) {
		this.gender = gender;
	}
	
	
	//1.3 获取age值的方法
	public int getAge() {
		return age;
	}
	
	public String getName() {
		return name;
	}
	
	public String getGender() {
		return gender;
	}
	
	public void printSelf() {
		System.out.println("name:"+name+" gender:"+gender+" age:"+age);
	}
	
}
package com.qf;

public class Phone {

	private String brand;
	private double price;
	private String cpu;
	private String ram;
	
	
	
	public String getBrand() {
		return brand;
	}
	public void setBrand(String brand) {
		this.brand = brand;
	}
	public double getPrice() {
		return price;
	}
	public void setPrice(double price) {
		this.price = price;
	}
	public String getCpu() {
		return cpu;
	}
	public void setCpu(String cpu) {
		this.cpu = cpu;
	}
	public String getRam() {
		return ram;
	}
	public void setRam(String ram) {
		this.ram = ram;
	}
	
	
	//重写toString
	
	
}
package com.qf;

public class Test01 {

	public static void main(String[] args) {
		Person p = new Person(18);
		p.printSelf();
		
		
		Person p2 = new Person();
		p2.setAge(17);
		p2.setName("pony");
		
		
		p2.printSelf();
		
		int age = p2.getAge();
		System.out.println();
		
		String name = p2.getName();
		System.out.println(name);
		
		
		Phone phone = new Phone();
		phone.setBrand("华为");
		phone.setPrice(3500);
		phone.setCpu("麒麟");
		phone.setRam("8G");
		
		System.out.println(phone);
		
	}
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/760077.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号