您的位置:首页 > 编程语言 > Java开发

bj java 初学2015-7-20

2015-07-20 19:19 375 查看

每日学习总结:

目录:

程序1:

程序2:

程序3:

程序4:

程序5:

程序6:

程序7:

程序1:在Test_a_1中调用Test_a类,实例化一个Test_a类对象,调用Test_a类下的say()和eat()方法

package com.day5_2015_07_20;
public class Test_a_1 {
public static void main(String[] args) {
Test_a lingzhuo = new Test_a();
lingzhuo.say("常灿光", "15071", "201216323", "男", 20);
lingzhuo.eat("面条");
}
}


package com.day5_2015_07_20;
public class Test_a {
String name;
String classes;
String xuehao;
String sexy;
int age;
public  void say(String n,String c,String xh,String s,int a)
{
name = n;
classes = c;
xuehao = xh;
sexy = s;
age = a;
System.out.println
("我的名字是:"+name+",在领卓"+classes+"上课。\n学号是:"+xuehao+",性别是:"+sexy+",今年:"+age+"岁了");
}
public void eat(String food)
{
System.out.println("我爱吃:"+food);
}
}


运行结果:



程序2:在Test_b_main中实例化Test_b_student类对象student和实例化Test_b_teacher类对象teacher,并调用各自的outStudentInformation/outTeacherInformation方法,输出详细信息。

package com.day5_2015_07_20;
public class Test_b_main {
public static void main(String[] args) {
Test_b_student student = new Test_b_student();
student.outStudentInformation("常灿光", "领卓15071班", "201216323", 20, "信息工程学院");
System.out.println("---------------");
Test_b_teacher teacher = new Test_b_teacher();
teacher.outTeacherInformation("韩顺平","2015071501" , 42, "软件学院");
}
}


package com.day5_2015_07_20;
public class Test_b_student
{
String name;
String banji;
String xuehao;
int age;
String xueyuan;
public void outStudentInformation(String xm,String bj,String xh,int nl,String xy)
{
name  = xm;
banji = bj;
xuehao = xh;
age = nl;
xueyuan = xy;
System.out.println("我的姓名是:"+name+"\n我的班级是:"+banji+"\n我的学号是:"+xuehao+"\n我的年龄是:"+age+"岁\n我的学院是:"+xueyuan);
}
}


package com.day5_2015_07_20;
public class Test_b_teacher
{
String name;
String gonghao;
int age;
String xueyuan;
public void outTeacherInformation(String xm,String gh,int nl,String xy)
{
name  = xm;
gonghao = gh;
age = nl;
xueyuan = xy;
System.out.println("我的姓名是:"+name+"\n我的工号是:"+gonghao+"\n我的年龄是:"+age+"岁\n我的学院是:"+xueyuan);
}
}


运行结果:



程序3:封装一个类,调用这个类的setter和getter方法,输出详细信息

package com.day5_2015_07_20;

public class Test_c_encapsulation {
private String time;
private String place;
private String people;
public String getTime() {
return time;
}
public void setTime(String time) {
//this 调用此方法的该类的对象
this.time = time;
}
public String getPlace() {
return place;
}
public void setPlace(String place) {
this.place = place;
}
public String getPeople() {
return people;
}
public void setPeople(String people) {
this.people = people;
}
public static void main(String[] args)
{
Test_c_encapsulation enExample = new Test_c_encapsulation();
enExample.setTime("2015-7-20");
enExample.setPlace("北京市昌平区柴禾市大街2号院领卓教育");
enExample.setPeople("华北水利水电大学信息工程学院网络工程专业2012163班学生");
System.out.println("时间是:"+enExample.getTime()+"\n地点是:"+enExample.getPlace()+"\n人物是:"+enExample.getPeople());
}
}


运行结果:



程序4:创建两个类Test_d_extends_animal和Test_d_extends_sheep,其中Test_d_extends_sheep继承自Test_d_extends_animal。在主方法Test_d_extends_main中实例化Test_d_extends_sheep对象sheep,并输出相关信息

package com.day5_2015_07_20;
public class Test_d_extends_main {
public static void main(String[] args) {
Test_d_extends_sheep sheep = new Test_d_extends_sheep();
sheep.setLegs(4);
sheep.setFood("草");
sheep.setJiaosheng("咩...咩...");
System.out.println("我有"+sheep.getLegs()+"条腿\n我的食物是"+sheep.getFood()+"\n我爱"+sheep.getJiaosheng()+"的叫");
sheep.run();
}
}


package com.day5_2015_07_20;
public class Test_d_extends_animal {
private int legs;
private String jiaosheng;
private String food;
public String getJiaosheng() {
return jiaosheng;
}
public void setJiaosheng(String jiaosheng) {
this.jiaosheng = jiaosheng;
}
public String getFood() {
return food;
}
public void setFood(String food) {
this.food = food;
}
public int getLegs() {
return legs;
}
public void setLegs(int legs) {
this.legs = legs;
}
//父类的构造方法
public Test_d_extends_animal()
{
//在父类中,但是调用的是该sheep对象的run方法,而不是父类(下面的)run()方法
this.run();
System.out.println("父类animal的构造方法");

}
//父类的run方法
public void run()
{
System.out.println("奔跑吧,我的动物孩子们");
}
}


package com.day5_2015_07_20;
public class Test_d_extends_sheep extends Test_d_extends_animal{
//通过无参构造方调用有参构造方法
public Test_d_extends_sheep()
{
this("懒洋洋");
}

public Test_d_extends_sheep(String name)
{
//默认调用父类的无参构造方法加不加无所谓,系统会默认调用
super();
System.out.println("子类sheep的构造方法----"+name);
//调用该方法的对象的方法
this.run();

}
public void run()
{
super.run();
System.out.println("奔跑吧,兄弟们,我是你们的喜羊羊");
}
}![这里写图片描述](http://img.blog.csdn.net/20150720183059450)


运行结果:



本程序中重点是:this关键字的使用

this.属性 该对象的属性

this.method 该方法的对象的方法

this() 必须第一行写,调用本类另一个构造器

程序5:比较equals()和==的用法

package com.day5_2015_07_20;
public class Test_e {
public static void main(String[] args) {
Integer i = new Integer(10);
Integer j = new Integer(10);
System.out.println("i==j吗?-->"+(i==j));//false
System.out.println("i.equals(j)吗?-->"+i.equals(j));//true
//10自动装包
Integer k = 10;//自动装包    Integer k = new Integer(10)
System.out.println("j==k吗-->"+(j==k));//false
int h = new Integer(10);//Integer这个对象自动拆包  相当于int h = 10
int g = new Integer(10);
System.out.println("h==g吗?-->"+(h==g));//true
System.out.println("k==h吗?-->"+(k==h));//true  k自动拆包
Boolean b = new Boolean(true);
Boolean b2 = new Boolean(true);
System.out.println("b==b2吗?-->"+(b==b2));//false
String s1 = "abc";
String s2 = "abc";
String s3 = new String("abc");
String s4 = new String("abc");
System.out.println("s1==s2吗?-->"+(s1==s2));//true
System.out.println("s1==s3吗?-->"+(s1==s3));//false
System.out.println("s2.equals(s4)吗?-->"+(s2.equals(s4)));//true
}

}


运行结果:



6:访问修饰符



7:方法重写和方法重载

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: