《java入门第一季》之面向对象(成员方法)

/*
	类的组成:成员变量,成员方法
	又加入了一个新的成员:构造方法。
	以后再提(类的组成):
		成员变量
		构造方法
		成员方法
			根据返回值:
				void类型
				非void类型
			形式参数:
				空参方法
				非空参方法
*/
class Student {
	public String getString() {
		return "helloworld";
	}

	public void show() {
		System.out.println("show");
	}
	
	public void method(String name) {
		System.out.println(name);//林青霞
	}
	
	public String function(String s1,String s2) {
		return s1+s2;//字符串拼接
	}
}

class StudentDemo {
	public static void main(String[] args) {
		//创建对象
		Student s = new Student();
		
		//调用无参无返回值方法
		s.show();//show
		
		//赋值调用无参有返回值方法
		String result = s.getString();
		System.out.println(result);//helloworld
		
		//调用带参无返回值的方法
		s.method("林青霞");//林青霞
		
		//调用带参带返回值的方法
		String result2 = s.function("hello","world");
		System.out.println(result2);//helloworld
	}
}


/*
	一个标准代码的最终版。
	
	学生类:
		成员变量:
			name,age, sex
		构造方法:
			无参,带三个参
		成员方法:
			getXxx()/setXxx()
			show():输出该类的所有成员变量值
			
	给成员变量赋值:
		A:setXxx()方法
		B:构造方法
		
	输出成员变量值的方式:
		A:通过getXxx()分别获取然后拼接
		B:通过调用show()方法搞定
*/
class Student {
	//姓名
	private String name;
	//年龄
	private int age;
	//性别
	private String sex;	
	//构造方法
	public Student() {
	}
	
	public Student(String name,int age,String sex) {
		this.name = name;
		this.age = age;
		this.sex = sex;
	}
	
	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;//private定义的成员变量只能在本类中修改
	}
	
	public int getAge() {
		return age;
	}
	
	public void setAge(int age) {
		this.age = age;
	}
	
	public String getSex(){
		return sex;
	}
	public void setSex(String sex){
		this.sex = sex;
	}
	
	//输出所有的成员变量值
	public void show() {
		System.out.println(name+"---"+age+"---"+sex);//本类中可以访问这些私有化的成员变量
	}
}

//测试类
class StudentTest {
	public static void main(String[] args) {
		//方式1给成员变量赋值
		//无参构造+setXxx()
		Student s1 = new Student();
		s1.setName("林青霞");
		s1.setAge(27);
		s1.setSex("女");
		//输出值
		System.out.println(s1.getName()+"---"+s1.getAge()+"---"+s1.getSex());
		s1.show();//调用该类中的方法
		System.out.println("----------------------------");
		
		//方式2给成员变量赋值
		Student s2 = new Student("刘意",30,"男");
		System.out.println(s2.getName()+"---"+s2.getAge()+"---"+s2.getSex());
		s2.show();
	}
}

通过get和set赋值

/*
	标准的手机类练习
	
	手机类:
		成员变量:brand,price,color
		构造方法:无参构造
		成员方法:getXxx()/setXxx()
*/
//方法一:利用getXxx()获取数据和setXxx()赋值,利用拼接输出相应的属性

//定义手机类
class Phone{
	//品牌
	private String brand;
	//价格
	private int price;
	//颜色
	private String color;
	//无参构造方法
	public Phone(){
	}
	
	public String getBrand(){
		return brand;
	}
	public void setBrand(String brand){
		this.brand = brand;
	}
	
	public int getPrice(){
		return price;//返回int类型的成员变量price的值
	}
	public void setPrice(int price){
		this.price = price;
	}
	
	public String getColor(){
		return color;
	}
	public void setColor(String color){
		this.color = color;
	}
}

class PhoneTest{
	public static void main(String[] args){
		//创建对象
		Phone p = new Phone();
		p.setBrand("华为");
		p.setPrice(2999);
		p.setColor("黑色");
		
		System.out.println(p.getBrand()+"--"+p.getPrice()+"--"+p.getColor());
	}
}


通过带参构造赋值:

/*
	标准的手机类练习
	
	手机类:
		成员变量:brand,price,color
		构造方法:无参构造
		成员方法:getXxx()/setXxx()
*/
//方法二,利用构造多个参数方法,调用show输出所有属性

//定义手机类
class Phone{
	//品牌
	private String brand;
	//价格
	private int price;
	//颜色
	private String color;
	//无参构造方法
	public Phone(){
	}
	
	public Phone(String brand,int price,String color){//注意这里是public Phone()
		this.brand = brand;
		this.price = price;
		this.color = color;
	}
	public void show(){//注意这里show无返回值,加上return否则报错
		System.out.println(brand+"---"+price+"---"+color);
	}
}

class PhoneTest2{
	public static void main(String[] args){
		//创建对象
		Phone p = new Phone("华为",2999,"黑色");
		p.show();
	}
}	









原文地址:https://www.cnblogs.com/wanghang/p/6299879.html