封装的相关例题

本文通过三个实例——Student、Dog和Book类,详细讲解了Java中的封装概念,包括属性私有化、get/set方法、构造函数的使用,并展示了如何在主函数中实例化对象、赋值和调用方法。对于每个类,都提供了无参和有参构造方法,并在Test类中进行了测试。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、封装一个类Student 
    (1) 属性:学号、名字、年龄、性别 
    (2) 方法:吃饭、跑步、睡觉 
    (3) 属性私有化 
    (4) 添加访问属性的 get 和 set 方法。 
    (5) 在类中添加构造方法
        无参构造方法
        有参构造方法-在构造方法中初始化学生的各个属性值
    (6) 创建类 Test 中添加主函数 
    (7) 在主函数中实例化 2 个 Student  类的对象,分别给属性赋值 
        其中一个对象直接使用带参构造方法创建
        1个对象使用无参构造方法创建,创建后使用set方法给属性赋值
    (8) 输出并调用所有的方法
    (9) 输出每个对象的详细信息

 方法区:

public class Student {
//属性
	private int xuehao;
	private String name;
	private int age;
	private String sex;
	
	
	//方法
	//无参
	public Student() {
		
	}
	//有参
	public  Student(int xuehao,String name,int age,String sex) {
		this.xuehao=xuehao;
		this.name=name;
		this.age=age;
		this.sex=sex;
	}
	
	public  static void wan() {
		System.out.println("玩");
	}
	
	public void eat	() {
		System.out.println(name+"爱吃饭");
	}
	public void sleep() {
		System.out.println(name+"爱睡觉");
	}
	public void run() {
		System.out.println(name+"爱跑步");
	}
	public void setXuehao(int xuehao) {
		this.xuehao=xuehao;
	}
	public int getXuehao() {
		return xuehao;
	}
	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 setSex(String sex) {
		this.sex=sex;
	}
	public String getSex() {
		return sex;
	}
	public String getInfo() {
		return "姓名:"+name+"学号:"+xuehao+"性别:"+sex+"年龄:"+age;
	}
}

测试区: 

public class StudentTest {
	public static void main(String[] args) {
		
		//夏洛
		Student xialuo=new Student(1234567,"夏洛",18,"男");
		System.out.println(xialuo.getInfo());
		xialuo.eat();
		xialuo.sleep();
		xialuo.run();
		
		
		//马冬梅
		Student m=new Student();
		m.setXuehao(7654321);
		m.setName("马冬梅");
		m.setAge(17);
		m.setSex("女");
		System.out.println(m.getInfo());
		m.eat();
		m.sleep();
		m.run();
		
		
		Student.wan();
	}

 2、封装一个类 Dog  
    属性有 name  type   color   age    sex  
    方法有  eat   run    sleep 
    属性私有化添加访问属性的get和set 方法  添加有参数和无参数的构造函数。
    创建类 Test 中添加主函数,在主函数中实例化3个Dog类的对象分别给属性赋值,输出并调用所有的

方法区:

public class Dog {
	private String name;
	private String type;
	private String color;
	private int age;
	private String sex;
	
	
	//无参
	public Dog() {
		
	}
	
	//有参
	

	public Dog(String name, String type, String color, int age, String sex) {
		this.name = name;
		this.type = type;
		this.color = color;
		this.age = age;
		this.sex = sex;
	}
	public void eat() {
		System.out.println(name+"爱吃");
	}
	public void run() {
		System.out.println(name+"爱跑步");
	}
	public void sleep() {
		System.out.println(name+"爱睡觉");
	}

	public String getName() {
		return name;
	}

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

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getColor() {
		return color;
	}

	public void setColor(String color) {
		this.color = color;
	}

	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 String getInfo() {
		return "昵称:"+name+"类型:"+type+"性别:"+sex+"年龄:"+age+"颜色"+color;
	}
}

 测试区:

public class DogTest {
public static void main(String[] args) {
	Dog g1=new Dog("夏洛","拉布拉多","黄色",10,"男");
	System.out.println(g1.getInfo());
	g1.eat();
	g1.run();
	g1.sleep();
	Dog g2=new Dog("马冬梅","萨摩耶","白色",9,"女");
	System.out.println(g2.getInfo());
	g2.eat();
	g2.run();
	g2.sleep();
	Dog g3=new Dog("袁华","哈士奇","黑白色",8,"男");
	System.out.println(g3.getInfo());
	g3.eat();
	g3.run();
	g3.sleep();
}
}

 3、封装一个类 Book
    属性有 图书编号 图书名称 作者 价格  
    获取图书信息的方法
    属性私有化添加访问属性的get和set 方法  添加有参数和无参数的构造函数。
    创建类 Test 中添加主函数,
    在主函数中创建一个数组,控制台输入5本图书信息存到数组中
    将所有的图书信息打印到控制台
    并获取所有图书的总价格

方法区:

public class Book {
	private int num;
	private String name;
	private String author;
	private double price;
	
	
	public Book(int num, String name, String author, double price) {
		this.num = num;
		this.name = name;
		this.author = author;
		this.price = price;
	}


	public Book() {
		
	}


	public int getNum() {
		return num;
	}


	public void setNum(int num) {
		this.num = num;
	}


	public String getName() {
		return name;
	}


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


	public String getAuthor() {
		return author;
	}


	public void setAuthor(String author) {
		this.author = author;
	}


	public double getPrice() {
		return price;
	}


	public void setPrice(double price) {
		this.price = price;
	}
	
//	public static void bianli(Book[]arr) {
//		for (int i = 0; i < arr.length; i++) {
//			System.out.println(arr[i]);
//		}
//	}
	public String getInfo() {
		return "书名"+name+"作者"+author+"编号"+num+"价格"+price;
	}
	
	public double zongjiage(Book[]arr) {
		double sum=0;
		for (int i = 0; i < arr.length; i++) {
			sum=sum+arr[i].getPrice();
		}
		return sum;
	}
	
}

测试区: 

public static void main(String[] args) {
		Book[] arr=new Book[5];
		Scanner sc=new Scanner(System.in);
		
		for (int i = 0; i < arr.length; i++) {
			System.out.println("请输入第"+(i+1)+"本书的信息");
			System.out.println("书名");
			String name=sc.next();
			System.out.println("编号");
			int num=sc.nextInt();
			System.out.println("作者");
			String author=sc.next();
			System.out.println("价格");
			double price=sc.nextDouble();
			Book book=new Book(num, name, author, price);
			arr[i]=book;
		}
		

//		for(Book book : arr) {
//			System.out.println(book.getInfo());
//		}
		for (int i = 0; i < arr.length; i++) {
			System.out.println(arr[i].getInfo());
		}
		
		
		//求价格
//		double sum=0;
//		for (int i = 0; i < arr.length; i++) {
//			sum=sum+arr[i].getPrice();
//		}
		double sum=arr[1].zongjiage(arr);
		System.out.println(sum);
	}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值