package com.neusoft.oop;
/**
* JAVA中的编程思想都是以面向对象来编程的。
* 1.什么是面向对象编程:面向对象编程核心就是面向类来编程
* 2.什么是类:是具有相同属性和行为的一个模板(可重用)
* 3.类是抽象出来的 而对象是一个具体的
* 4.每个对象都具有属性和行为,我们可以将这些具有相同的属性以及行为抽象出一个类
* 在这类中只能存在属性(变量)和行为(方法)
* @author ttc
*
*/
public class Student {
String sid;
String name;
String school;
public void study() {
System.out.println("study.....");
}
public void eat() {
System.out.println("eat.....");
}
public void game() {
System.out.println("game.....");
}
public static void main(String[] args) {
//如何去一个对象 语法:类名 变量名 = new 类名();
Student stu1 = new Student();
stu1.name="zhangsan";
System.out.println(stu1.name);
stu1.study();
Student stu2 = new Student();
stu2.name="lisi";
System.out.println(stu2.name);
stu2.study();
}
}
package com.neusoft.oop;
/**
* 类名=========表名
* 属性=========列名
* Java Bean:就是一个普通的JAVA类,只包含GET/SET方法
*/
public class Emp {
private int empno;
private String ename;
private String job;
private double salary;
public Emp(int empno, String ename) {
this.empno = empno;
this.ename = ename;
}
public int getEmpno() {
return empno;
}
public void setEmpno(int empno) {
this.empno = empno;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public static void main(String[] args) {
Emp e1 = new Emp();//与类相同
e1.setEmpno(12);
e1.setEname("tom");
e1.setJob("salsar");
e1.setSalary(5000);
}
}
package com.neusoft.oop;
/**
* 匿名对象
* new在堆中分配空间
* @author Administrator
*
*/
public class NonOOP326 {
public void test(){
System.out.println("test......");
}
public static void main(String[] args) {
//调用了同一个对象NonOOP326中的test()方法
NonOOP326 NonOOP326 = new NonOOP326();
NonOOP326.test();
NonOOP326.test();
//调用了不同对象中的test()方法,每new一次都会在内存中产生一个新的对象
new NonOOP326().test();//匿名对象
new NonOOP326().test();
}
}
package com.neusoft.oop;
import java.util.concurrent.SynchronousQueue;
public class MethodDemo {
public int method_1(int a) {
return 0;
}
public String method_2(String name) {
return name;
}
public String[] method_3(String[] array) {
return array;
}
public Student method_4(Student stu) {
return stu;
}
public static void main(String[] args) {
MethodDemo mDemo = new MethodDemo();
int a = mDemo.method_1(1);
System.out.println(a);
System.out.println("------------------------------------");
String name = mDemo.method_2("tom");
System.out.println(name);
System.out.println("----------------------------------------");
String[] array = { "tom","jerry","lucky" };
String[] arr = mDemo.method_3(array);
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
System.out.println("--------------------------------------------------");
Student stu = new Student();
stu.name = "tom";
Student student = mDemo.method_4(stu);
System.out.println(student.name);
}
}