Learning java, day5 (object class and overloaded)

Learning java, day5 (object class and overloaded)

1. Follow the class design, i.e. the members of the design categories: properties, methods
instantiate the class 2. The object class is created. (Eg: Person p = new Person, where P is the object)
3. Through the "Object . property. "" Object methods "implementation.

/**
 * Students
 * @author Reishinj
 *
 */
public class Student {
/**
*类的属性
*/
	public String name;//姓名
	public int age;//年龄
	public String courses;//课程
	public String interest;//兴趣
	/**
	 * 显示学生的个人信息
	 */
	public void showInfo() {// 类的方法
	
		System.out.println("姓名:" + name);
		System.out.println("年龄:" + age);
		System.out.println("课程:" + courses);
		System.out.println("兴趣:" + interest);
	}
}
public class Test{
	public static void main(String[] args){
	
			Student stu = new Student();	// 创建对象
			stu.name = "小明"	;	
			stu.age = 12;
			stu.courses = "语文,数学,英语";
			stu.interest = "打球,上网";
			stu.showInfo();
	}
}

Q1:. Use of object-oriented (Baidu), design class Circle, calculate the area of a circle
to suspend his thought for a moment, to write a line of
public Double (the X-, the y-) {
return the X-= 3.14
}
entirely who I am where I am I'm doing
after listening to the answers to get the code as follows:

/**
*计算圆的面积 r是圆的半径
*/
public class Circle{//创建类 Cricle
	public double area(r){//类的方法,因为π的值带小数点,所以使用双精度的double
		return 3.14 * r *r;//返回值为计算方法3.14*半径的平方
	}
}

When called, also uses an anonymous object's methods

public class Test{
	public static void main(String[] args){
		
		double ares = new double.Circle().ares();//ares括号内填入半径
				System.out.println(ares);//这里打印的是上一行ares括号内填入的值并在类Circle中使用ares的方法计算并且return π*r2回来的值
	}
}

A plurality of the same name if you want to coexist in a method of the class, then the data must be the same method on the number or type of parameters different parameters
such as a method with the same name is called overloading (not the same parameter name)

	public class Test{
		public int add(int x, int y) {
			return x + y;
		}
		
		public double add(int x, double y) {
			return x + y;
		}
		public int add(int x, int y, int z) {
			return x + y + z;
		}
	}

In the above code, if
public int the Add (int m, n-int) {
return + n-m
}
can not reload configuration

Q3: programming, define three overloaded methods and calls a method named mol.
1. decomposition method accepts three parameters int a, int two parameters, the parameters are a string performs a square operation and outputs the result, and multiplying. output result, information output string
2. in the main class of the main () method call parameter region are three methods, respectively.

public class Bubble {
		public void mOL(int i) {
			System.out.println(i * i);
		}
		public void mOL(int i, int j) {
			System.out.println(i * j);
			
		}
		public void mOL(String r) {
			System.out.println(r);
		}
		}

The main class to call, although the same name, but using alt + / time will be displayed separately.

Q4: definition of three overloaded method max () method of selecting the maximum value of a two int values, the second method of selecting the maximum value of the two double values.
The third method of seeking three double values maximum, and call methods.

public class Boubble{
		public void max(int i, int j) {
			if(i > j) {
				System.out.println("最大值是:" + i);
			}else {
				System.out.println("最大值是:" + j);
			}
			
		}
		
		public void max(double i, double j) {
			if(i > j) {
				System.out.println("最大值是:" + i);
			}else {
				System.out.println("最大值是:" + j);
			}
		}
		
		public void max(double i, double j, double k) {
			if(i > j) {
				if(i > k) {
					System.out.println("最大值是:" + i);
				}else {
					System.out.println("最大值是:" + k);
				}
			}else{
				if(j > k) {
					System.out.println("最大值是:" + k);
				}else {
					System.out.println("最大值是:" + j);
				}
			}
		}
		}

In the above code, you can create a new variable, and then replace the if statement to print variable
values can be last time to print variable.
But I heard that uses memory, I do not know if there are big brothers can answer.

PS: Write semicolon, the semicolon to write, write, add a semicolon int, int plus, plus int create method is a method call or to lose value creation methods do not directly lose value!!!

Released eight original articles · won praise 0 · Views 84

Guess you like

Origin blog.csdn.net/Reishinj/article/details/104977476