Java中的继承

时间:2016-02-18 02:48:13

标签: java inheritance methods subclass superclass

我正在学习如何在Java中使用继承。此程序的目的是编写包含有关某些日志条目的信息的类,并提供设置和获取适当信息的方法。如果需要更正,请告诉我。

ArithmeLog.java

//super class ArithmeLog with three functions to capture three instances

import java.util.*;

import java.text.*;

public class ArithmeLog {

 public ArithmeLog() {

 }



 public void captid1(String a) {

      System.out.println("");

 }



 public void captid2(String SongName) {

      System.out.println("");

 }

 public void Date() {

      Date capttim = new Date( );

      SimpleDateFormat formt = new SimpleDateFormat

 ("E yyyy.MM.dd 'at' hh:mm:ss a zzz");

      System.out.println("Time is : " +

formt.format(capttim));

   }

    }

SongLog.java

//class SongLog which inheriting ArithmeLog class

import java.util.*;

import java.text.*;

public class SongLog extends ArithmeLog {

 public SongLog() {

      super();

      System.out.println("");

 }



 @Override

 public void captid1(String SongID) {

      System.out.println("SongID is: "+SongID);

 }



 @Override

 public void captid2(String SongName) {

      System.out.println("Song name is: "+SongName);

 }

 @Override

 public void Date() {

      Date capttim = new Date( );

      SimpleDateFormat formt = new SimpleDateFormat

  ("E yyyy.MM.dd 'at' hh:mm:ss a zzz");

      System.out.println(" Date and time is : " +

  formt.format(capttim));

 }

 }

 AdLog.java

// AdLog class which inheriting ArithmeLog class

import java.util.*;

import java.text.*;

 public class AdLog extends ArithmeLog {

 public AdLog() {

      super();

      System.out.println("");

 }



 @Override

 public void captid1(String AdvertisementID) {

 System.out.println("AdvertisementID is:"+AdvertisementID);

 }



 @Override

 public void captid2(String AdvertiserID) {

      System.out.println("AdvertiserID is:"+AdvertiserID);

 }

 @Override

 public void Date() {

      Date capttim = new Date( );

      SimpleDateFormat formt = new SimpleDateFormat

 ("E yyyy.MM.dd 'at' hh:mm:ss a zzz");

      System.out.println(" Date and time is : " +

 formt.format(capttim));

 }

 }

  Mainpgm.java

// main class creating objects of classes and calling methods

public class Mainpgm {

 public static void main(String[] args) {

      //creating first object of classes

      ArithmeLog animal = new ArithmeLog();

      AdLog ao1 = new AdLog();

      SongLog so1 = new SongLog();

      System.out.println();

      ao1.captid1("1");

      ao1.captid2("7");

      ao1.Date();

      so1.captid1("1");

      so1.captid2("songg1");

      so1.Date();

      //creating second object of classes

      AdLog ao2 = new AdLog();

      SongLog so2 = new SongLog();

      ao2.captid1("2");

      ao2.captid2("ad2");

      ao2.Date();

      so2.captid1("2");

      so2.captid2("songg2");

      so2.Date();

      //creating third object of classes

      AdLog ao3 = new AdLog();

      SongLog so3 = new SongLog();

      ao3.captid1("3");

      ao3.captid2("ad3");

      ao3.Date();

      so3.captid1("6");

      so3.captid2("songg3");

      so3.Date();



    }

  }

1 个答案:

答案 0 :(得分:1)

根据您的评论,您是否在1个文件中拥有所有这些内容?

每个'section'应该在它自己的文件中。所以一个名为SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit(); editor.putString("name", "Elena"); editor.putInt("idName", 12); editor.commit(); 的文件,该行下面的所有内容都在SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE); String restoredText = prefs.getString("text", null); if (restoredText != null) { String name = prefs.getString("name", "No name defined");//"No name defined" is the default value. int idName = prefs.getInt("idName", 0); //0 is the default value. ,然后是一个名为ArithmeLog.java的文件,然后该文件包含其下的所有内容,等等。