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

Java个人学习笔记(二)

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

Java个人学习笔记(二)

8. Objects

        Bring together related variables and methods into a single thing;

        Make it much easier to build large complex programs;

 name = new (, , ...);
//arg means arguments
8.1 Using Objects
import javax.swing.Jframe;

public class CreateWindow {
	
	public static void main(String[] args) {
		Jframe f = new Jframe();//create a new project named f
		f.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);//create a window
		f.setSize(500,500);//window's size
		f.setVisible(true);//visible window
	}
}
8.2 Creating Objects and Using It

        Programs below are in one folder but two files.

public class Person {
	//two attribute
	public String name;
	public int age;
	//a constructor
	public Person(String s, int a) {
		name = s;
		age = a;
	}
	//a method
	public void sayHello() {
		System.out.println("Hi, I'm " + name + " and I'm " + age + " years old.");
	}	
}
public class text {
	
	public static void main(String[] args) {
		//make a person
		Person personOne = new Person("Bill", 56);
		//call its method
		personOne.sayHello();
		//we can also access its attributes
		System.out.println(personOne.name);
		//or set them
		personOne.age = 57;
		personOne.sayHello();
		//we can make another now
		Person personTwo = new Person("Fred", 1);
		personTwo.sayHello();	
	}
}
8.3 Differences between Public, Private and Protected

        public: All classes, attributes and methods are visible and can be used by anything;

        private:Can only be used by the class in which they are defined

        protected: Can be used by other classes in the same package or that inherit from this class     

8.4  Private, Getter and Setter

        Programs below are in one folder but two files.

public class Person2 {
	//two attributes
	private String name;
	private int age;
	//a constructor
	public Person2(String s, int a) {
		name = s;
		age = a;
	}
	//getter and setter of name
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	//getter and setter of age
	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}
	//a method
	public void sayHello() {
		System.out.println("Hi, I am " + name + " and I am " + age + " years old.");
	}
}

        Getter and setter can be quickly created by Source → Generate Getters and Setters

public class test2 {
	public static void main(String[] args) {
		Person2 WDY = new Person2("Cooper", 23);
		WDY.sayHello();//Hi, I am Cooper and I am 23 years old.
		
		WDY.setAge(34);//use a public method(setter) to change the private variable
		WDY.sayHello();//Hi, I am Cooper and I am 34 years old.
		
		System.out.println(WDY.getName());
        //use a public method(getter) to get the value of private variable
	}
}
8.5 This

        We can use this anywhere in a class to refer to class attributes (and method)

public class Circle{
    private double radius;
    public Circle(radius){
        this.radius = radius;
    }
}
8.6 Static*

        It is an optional keyword that allows us to have class attributes and methods that can be accessed without creating an instance of the class (for example, we can call a method in main method without creating an object)

public class Static {
	//this method is not static type, so it can not be called directly in main method
	public int addingMethod(int fN, int sN) {
		int result = fN + sN;
		return result;
	}
	
	public static void main(String[] args) {
		int a = 3;
		int b = 4;
		int c = 5;
		int d = 6;
		//there is not constructor in object, so java will create a default constructor
		Static addingObject = new Static();
		
		int aPlusb = addingObject.addingMethod(a, b);
		System.out.println(aPlusb);
	}
}

        Static attributes can be accessed without making an instance of the class. 

        Programs below are in one folder but two files.

public class Person3 {
	//two attributes
	public String name;
	public static int age;//age is now static
	//a constructor
	public Person3(String s, int a) {
		name = s;
		age = a;
	}
	//a method
	public void sayHello() {
		System.out.println("Hi, I am " + name + " and I am " + age + " years old.");
	}
}
public class test3 {
	
	public static void main(String[] args) {
		//make a person
		Person3 personOne = new Person3("Bill", 56);
		//call its method
		personOne.sayHello();
		//we can also access its attributes
		System.out.println(personOne.name);
		//or set them
		//personOne.age = 57;//error
		personOne.sayHello();
		//we can make another now
		Person3 personTwo = new Person3("Fred", 1);
		personTwo.sayHello();	
		personOne.sayHello();//Bill is 1 now!!!!	
	}
}
//static attribute is used in each object
8.7 Objects and References

        The following command creates two things, on object and a reference

Person personOne = new Person("Bill", 56);

         References are just like the variables we've seen before but now they store the address of the object they refer to

8.8 Object and Scope

        References follow all of the rules about scope that we've covered preiously

        Objects are available anywhere in our program

        Each reference refers to only one object

        Many references can refer to the same object

public class Scope {
	public static void main(String[] args) {
		Person2 personOne = new Person2("Bill", 56);
		Person2 personTwo = personOne;
		
		personTwo.sayHello();//Hi, I am Bill and I am 56 years old.
		personTwo.setAge(57);
		personTwo.sayHello();//Hi, I am Bill and I am 57 years old.
		personOne.sayHello();//Hi, I am Bill and I am 57 years old.
	}
}
//personOne is an address(reference) of the object(Bill, 56)
//personTwo is other address(reference)
//so it is new reference but not new object
8.9 Passing Objects to Methods 

        We don't need to pass the object, just tell the method the reference

        Programs below are in one folder but two files.

public class Circle2 {
	private double radius;
	
	public Circle2(double r) {
		radius = r;
	}
	
	public double getRadius() {
		return radius;
	}

	public void setRadius(double radius) {
		this.radius = radius;
	}

	public double compArea() {
		return 3.14152 * radius * radius;
	}
	
	public double compCircle() {
		return 3.14152 * 2 * radius;
	}
	
	public void describe() {
		System.out.println("This Circle has:");
		System.out.println("tradius " + radius);
		System.out.println("tarea " + compArea());
		System.out.println("tcircumference " + compCircle());
	}
}
public class testCircle2 {
	public static void main(String[] args) {
		Circle2 c = new Circle2(2.5);
		c.describe();
		doubleArea(c);
		c.describe();
	}
	//double the area of a circle
	public static void doubleArea(Circle2 d) {
		double radius = d.getRadius();
		radius *= Math.sqrt(2.0);//calculate the root of 2.0
		d.setRadius(radius);
	}
}
//c is in main scope, d is in doubleArea scope, two reference but one object
8.10 Overloading and Constructors

        Method can be overloaded(same name, different arguments)

        This is often useful for constructors

8.11 ==, Equals Method and toString Method 8.11.1 ==

        Programs below are in one folder but two files.

public class Person {
	//two attributes
		private String name;
		private int age;
		//a constructor
		public Person(String s, int a) {
			name = s;
			age = a;
		}
		//getter and setter of name
		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}
		//getter and setter of age
		public int getAge() {
			return age;
		}

		public void setAge(int age) {
			this.age = age;
		}
		//a method
		public void sayHello() {
			System.out.println("Hi, I am " + name + " and I am " + age + " years old.");
		}
}
public class EqualTest {
	public static void main(String[] args) {
		Person personOne = new Person("Cooper", 23);
		Person personTwo = personOne;
		boolean test1 = (personOne == personTwo);//true
		Person personThree = new Person("Cooper", 23);//create a new object
		boolean test2 = (personOne == personThree);//false
	}
}
//personOne & personTwo: one object with two references
//personOne & personThree: two objects

         Strings  are objects

public static StringComparsion{
    public static void main(String[] args){
        String a = "Hello";
        String b = "He";
        b += "llo";
        boolean test1 = (a == b);//false

        String c = "Hello";
        boolean test2 = (a == c);//true, because java is efficient and only makes
                                 //one object that a and c both refer to
8.11.2 String.equals and toString

        All object have an equals method

        In Strings, the method has been overwritten to check the characters in the String 

        Wrtting a toString() in any object tells Java what to do should it be asked to interpret the object as a String (e.g. in a println statement)

public class EqualMethod {
	private String name;
	private int age;
	
	public EqualMethod(String n, int a) {
		name = n;
		age = a;
	}
	
	public int getAge() {
		return age;
	}
	
	public String getName() {
		return name;
	}
	
	public boolean equals(Object o) {
		//
		if(!(o instanceof EqualMethod)) {
			return false;
		}else {
			EqualMethod person = (EqualMethod) o;
			if(age == person.getAge() && name.equals(person.getName())) {
				return true;
			}else {
				return false;
			}
		}
	}
	
	public String toString() {
		return "Name:" + name + " Age:" + age;
	}
	
	public static void main(String[] args) {
		EqualMethod a = new EqualMethod("Cooper", 23);
		EqualMethod b = new EqualMethod("Dongyao", 23);
		EqualMethod c = new EqualMethod("Cooper", 23);
		
		System.out.println(a == b);//false! Different objects
		System.out.println(a.equals(c));
		//true! if there is not line 20 to 31
        //it will be false because different objects 
		
		System.out.println(a);//Name:Cooper Age:23
		System.out.println(b);//Name:Dongyao Age:23
	}
}
8.12 Inheritance

        Make classes that inherit everything from another class and (optionally) add extra attribute and methods

        Programs below are in one folder but three files.

public class Bin {
	private String contents;
	public Bin() {
		contents = "";
	}
	public void add(String s) {
		contents += s + ", ";
	}
	public String toString() {
		return "Bin contains: " + contents;
	}
	public void empty() {
		contents = "";
	}
}
//  means inheritance
public class RecylingBin extends Bin{
	
	private String type;
	public RecylingBin(String type) {
		super();//call the constructor from class of Bin (must be first line)
		this.type = type; 
	}
	
	public String toString() {
		return "RecylingBin contains" + "(" + type + "):" + contents;
	}
}
//child class can add its own argument and method
public class TestBin {
	public static void main(String[] args) {
		
		Bin normalBin = new Bin();
		System.out.println(normalBin);//Bin contains:
		normalBin.add("paper");
		System.out.println(normalBin);//Bin contains: paper, 
		normalBin.add("coke can");
		System.out.println(normalBin);//Bin contains: paper, coke can,
		normalBin.empty();
		System.out.println(normalBin);//Bin contains: 
		
		RecylingBin recylingBin = new RecylingBin("Large");

		System.out.println(recylingBin);//RecylingBin contains(type): 
		recylingBin.add("paper");
		System.out.println(recylingBin);//RecylingBin contains(type): paper, 
		recylingBin.add("coke can");
		System.out.println(recylingBin);//RecylingBin contains(type): paper, coke can, 
		recylingBin.empty();
		System.out.println(recylingBin);//RecylingBin contains(type):
	}
}

        Inheritance can go on forever

        Prgram below is an inheritance of RecylingBin which is metioned above

//  means inheritance
public class RecylingBin2 extends RecylingBin{

	public RecylingBin2(String type) {
		super("Large");//must enter an argument for the constructor
	}
}

 

         

        

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/396729.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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