南昌大学技术交流...吧 关注:89贴子:975
  • 9回复贴,共1

12月1日安卓部上课代码及讨论区

只看楼主收藏回复

面向对象


IP属地:浙江1楼2015-12-01 22:07回复
    例:
    创建一个老师类
    类:老师
    ClassTeacher{
    属性(变量)
    构造方法
    方法(函数)
    }
    对象:x老师
    New Teacher() //每个被new出来的对象都具有该类里面的属性


    IP属地:浙江2楼2015-12-01 22:08
    回复
      代码1
      package text;
      public class test1 {
      publicstatic void main(String[] args){
      Teacherteacher = new Teacher();
      Studentstudent = new Student(21,32);
      teacher.tea();
      System.out.println(teacher.age);
      System.out.println(student.age);
      System.out.println(student.cla);
      }
      }
      class Teacher{
      publicint age;
      publicTeacher(){
      age= 2;
      }
      voidtea(){
      System.out.println("jiaoshu");
      }
      }
      class Student
      {
      publicint age;
      publicint cla;
      publicStudent(int age,int cla){
      this.age= age;
      this.cla= cla;
      }
      voidstudy(){
      System.out.println("xuexi");
      }
      }


      IP属地:浙江3楼2015-12-01 22:08
      回复
        代码2
        package text;
        public class task2 {
        public static void main(String[] args){
        People p = new People(10,11);
        Student1 s = new Student1(22,33);
        System.out.println(p.age+" "+p.height);
        System.out.println(s.age+" "+s.cla+" "+s.height+" "+s.num);
        p.eat();
        s.eat();
        }
        }
        class People{
        int age;
        int height;
        public People(int a,int h){
        age = a;
        height = h;
        }
        public void eat(){
        System.out.println("吃.......");
        }
        }
        class Student1 extends People{
        int cla;
        int num;
        public Student1(int a,int h){
        super(a,h);
        cla = a;
        num = h;
        }
        }


        IP属地:浙江4楼2015-12-01 22:37
        回复
          extends
          除了私有的其他的都会被继承下来


          IP属地:浙江5楼2015-12-01 22:38
          回复
            JAVA😳这一次在平行时空里展开一段新的旅程]
            [你留下我离开〕
            [我们永不永不说再见]
               --来自助手版贴吧客户端


            IP属地:江西来自Android客户端6楼2015-12-02 08:29
            收起回复