重写toString()方法
public class Student {
	String name;
	int age;
	public Student(String name, int age) {
		this.name = name;
		this.age = age;
	}
	public String toString() {
		return "我叫" + name + ",今年" + age + "岁。";
	}
	public static void main(String[] args) {
		Student s1 = new Student("张三", 16);
		System.out.println(s1);
		Student s2 = new Student("李四", 19);
		System.out.println(s2);
	}
}
//////////////////////////////////////////////////////////////////////////////////////
class Apple {
	String color;
	String name;
	double weight;
	double price;
	
	public Apple(String color, String name, double price, double weight) {
		this.color = color;
		this.name = name;
		this.weight = weight;
		this.price = price;
	}
	
	@Override
	public String toString() {
		return this.color + "的苹果被称为“" + this.name + "”, 每500克" + this.price + "元RMB,买了" + this.weight + "克,需支付" + (float)(this.price * (this.weight / 500)) + "元RMB。";
	}
}
public class HongxinFSApple {
	public static void main(String[] args) {
		Apple apple = new Apple("红色", "糖心富士", 4.98, 2500);
		System.out.println(apple.toString());
	}
}
 
                     
                    
                 
                    
                 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号